Unreal API & SDK Docs 3.0.0
This guide walks you through a simple "Hello, World" application that demonstrates the core concepts of PubNub in Unreal Engine:
- Setting up a connection to PubNub
- Sending a "Hello, World" message
- Receiving messages in real-time
Overview
You build a PubNub client in an Unreal Engine C++ project, set up an event listener, and send and receive a "Hello, World" message.
Prerequisites
Before we dive in, make sure you have:
- A basic understanding of Unreal Engine and C++
- Unreal Engine 5.0 to 5.8 installed. Fab supports versions 5.2 to 5.8. For versions 5.0 and 5.1, install the plugin from GitHub
- A PubNub account (we'll help you set this up!)
Setup
Get your PubNub keys
First things first – you'll need your PubNub keys to get started. Here's how to get them:
- Sign in or create an account on the Admin Portal and create an app.
- When you create a new app, the first set of keys is generated automatically.
- Find your publish and subscribe keys in the app dashboard.
We recommend that you create separate keysets for production and test environments.
Install the SDK
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
The PubNub Unreal Engine SDK is available on FAB as part of the PubNub Gaming SDK - a free downloadable plugin that you can add to your project. You can also get the plugin directly from GitHub.
-
Install the PubNub Gaming SDK plugin from FAB. For information on installing plugins, refer to the Unreal Engine documentation.
-
Enable the PubNubSDK in your project. For information on enabling plugins, refer to the Unreal Engine documentation.
-
In your project's Build.cs file, add the dependency (this is required only for using PubNub SDK with C++):
PrivateDependencyModuleNames.AddRange(new string[] { "PubnubLibrary" }); -
In files that you want to use PubNub SDK functionality include the necessary header:
#include "PubnubSubsystem.h"
Configure PubNub
The SDK has no Project Settings page. You create each PubNub client in code with CreatePubnubClient(). Pass your keys and User ID in an FPubnubConfig struct. The Publish Key, Subscribe Key, and User ID are required, and the SDK rejects empty values. Use the demo keys for this guide. For production, replace them with your own keys from the Admin Portal.
Required User ID
Always set the User ID to uniquely identify the user or device that connects to PubNub. Persist this User ID and keep it unchanged for the lifetime of the user or the device. CreatePubnubClient() fails if the User ID is empty.
Configure the Unreal Engine project
This example needs one C++ class. It creates the client, listens for messages, subscribes, and publishes.
Create a new project
Launch the Unreal Editor and create a new C++ project (or open your existing project).
Create an Actor class
- Click the Tools dropdown menu and select New C++ Class....
- Create a class that inherits from
Actor. - Place an instance of the class in your level.
Steps
Initialize PubNub
- Blueprint
- C++
Make sure to replace the demo keys with your app's publish and subscribe keys from the Admin Portal.
In your Actor header file:
1
In your Actor implementation file:
1
For more information, refer to the Configuration section of the SDK documentation.
Set up event listeners
Listeners help your app react to events and messages. You can implement custom app logic to respond to each type of message or event.
Bind a function to the OnMessageReceived delegate of your client. The listener logs each received message to the Output Log.
1
1
For more information, refer to the Event Listeners section of the SDK documentation.
Create a subscription
To receive messages sent to a particular channel, you need to subscribe to it.
Subscribe to a channel with your client:
1
1
For more information, refer to the Subscribe section of the SDK documentation.
Publish messages
When you publish a message to a channel, PubNub delivers that message to everyone who is subscribed to that channel.
A message can be any type of JavaScript Object Notation (JSON)-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB.
- Blueprint
- C++
Publish a message to a channel with your client:
1
1
For more information, refer to the Publish section of the SDK documentation.
Run the app
To test your Hello World PubNub integration:
- In the Unreal Editor, click the Play button in the toolbar.
- Look at the Output Log (Window > Developer Tools > Output Log).
- Trigger the function that creates the client, then the functions that subscribe and publish.
- In the Output Log, you should see the received message.
Cross-platform testing
To verify that your PubNub implementation works across platforms:
- Open the PubNub Debug Console in your web browser.
- Enter the demo publish and subscribe keys.
- Subscribe to the channel you used in your code.
- Publish a message from your Unreal application.
- The message should appear in both your Unreal app's logs and the PubNub Debug Console.
- You can also publish a message from the Debug Console and see it appear in your Unreal app's logs.
Complete example
The snippets in this guide come from the SDK repository. For complete, compilable sources, refer to Sample_Configuration and Sample_PubSub in Source/PubnubLibraryTests on GitHub.
Next steps
Great job! 🎉 You've successfully created your first PubNub application in Unreal Engine. Here are some exciting things you can explore next:
- Build chat
- Advanced features
- Real examples
- More help
- Learn about the Unreal Chat SDK for ready-to-use chat features.
- Implement user Presence to show who's online.
- Try out Presence to track online/offline status.
- Implement Message Persistence to store and retrieve messages.
- Use Access Manager to secure your channels.
- Implement Objects to manage users and channels metadata.
- Look at the Announcing PubNub's UE and Gaming Chat SDKs blog.
- Explore our GitHub repository for more code samples.
- Check out our SDK reference documentation for detailed API information.
- Email support@pubnub.com if you need help.
- Ask our AI assistant (the looking glass icon at the top of the page) for help.