For AI agents: this page in Markdown: https://www.pubnub.com/docs/sdks/unreal.md. Documentation index: https://www.pubnub.com/llms-full.txt.

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!)
icon

Installation on Unreal Engine <5.2.0

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.

  1. Install the PubNub Gaming SDK plugin from FAB. For information on installing plugins, refer to the Unreal Engine documentation.

  2. Enable the PubNubSDK in your project. For information on enabling plugins, refer to the Unreal Engine documentation.

  3. 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" });
  4. 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​

  1. Click the Tools dropdown menu and select New C++ Class....
  2. Create a class that inherits from Actor.
  3. Place an instance of the class in your level.

Steps​

Initialize PubNub​

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.

For more information, refer to the Publish section of the SDK documentation.

Run the app​

To test your Hello World PubNub integration:

  1. In the Unreal Editor, click the Play button in the toolbar.
  2. Look at the Output Log (Window > Developer Tools > Output Log).
  3. Trigger the function that creates the client, then the functions that subscribe and publish.
  4. In the Output Log, you should see the received message.

Cross-platform testing​

To verify that your PubNub implementation works across platforms:

  1. Open the PubNub Debug Console in your web browser.
  2. Enter the demo publish and subscribe keys.
  3. Subscribe to the channel you used in your code.
  4. Publish a message from your Unreal application.
  5. The message should appear in both your Unreal app's logs and the PubNub Debug Console.
  6. 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:

Was this page useful?

Last updated on