PubNub Unreal SDK 0.1.0

This page outlines the steps to follow to create a simple application with PubNub. This covers the basics of integrating PubNub in your application: setting up a connection to PubNub, and sending and receiving messages.

  1. PubNub account
  2. Download Unreal Engine and PubNub plugin
  3. Create user interface
  4. Add logic with Blueprints
Interactive Blueprints

This sample app is based on Blueprints.

You can copy any interactive Blueprint you find in the PubNub Unreal SDK documentation and paste it into your Unreal Editor's Blueprint editor.

PubNub account

Sign in or create an account to create an app on the Admin Portal and get the keys to use in your application.

When you create a new app, the first set of keys is generated automatically, but a single app can have as many keysets as you like. We recommend that you create separate keysets for production and test environments.

Download Unreal Engine

  1. Download and install Unreal Engine version 5.2 or higher.
icon

Installation on Unreal Engine <5.2.0

Download the SDK

PubNub Unreal Engine SDK is available on FAB as a free downloadable plugin that you can add to your project.

To download the Unreal SDK:

  1. Install the PubNub 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.

icon

Usage in Blueprints and C++

Configure the Unreal Engine project

  1. In Unreal Engine, create necessary classes like:

    ComponentDescription
    GameModeControls the game’s rules and flow.
    PlayerController (Widget)Manages players' inputs and the characters they control.
    PlayerPawnRepresents the player’s character in the game world.
    GameInstanceHandles data and logic across map transitions and beyond the lifetime of any single level.
    LevelContains your game world.
    WidgetManages user interfaces like menus or HUD elements.
  2. In Project Settings, on the Maps and Modes tab, specify the correct default GameMode and GameInstance.

    Default classes

    Maps and Modes is where you define what classes your project will use by default. This ensures that your custom classes are used when the game starts, which is fundamental for accessing your custom logic and settings.

  3. In the Default Modes section, change Player Controller Class and Default Pawn Class to the created ones.

    Default modes

    You need to link your custom PlayerController and DefaultPawn classes to the GameMode so that when the game starts, these specific classes will be used for the player character and their controller, providing custom functionalities.

  4. In the PlayerController Blueprint, create and add the widget to the viewport. Make sure to change GettingStarted_C to the widget you created.

For more information on Unreal Engine basics, refer to the Unreal Engine documentation.

Configure PubNub SDK

The minimum configuration to send and receive messages with PubNub are setting the Publish Key and Subscribe Key.

  1. In the Unreal Editor window, click the Settings dropdown and select Project Settings.

  2. In the Project Settings window, scroll down to the Plugins section and click Pubnub SDK.

  3. In the Plugins - Pubnub SDK view, provide the desired values for the available configuration options.

Open project settings

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

Create user interface

In your widget Blueprint, open the Designer view and paste the following code. This adds the UI elements we will use to publish and subscribe.

Begin Object Class=/Script/UMG.CanvasPanel Name="CanvasPanel_20" ExportPath=/Script/UMG.CanvasPanel'"/Game/UI/GettingStarted.GettingStarted:WidgetTree.CanvasPanel_20"'
Begin Object Class=/Script/UMG.CanvasPanelSlot Name="CanvasPanelSlot_1" ExportPath=/Script/UMG.CanvasPanelSlot'"/Game/UI/GettingStarted.GettingStarted:WidgetTree.CanvasPanel_20.CanvasPanelSlot_1"'
End Object
Begin Object Class=/Script/UMG.CanvasPanelSlot Name="CanvasPanelSlot_0" ExportPath=/Script/UMG.CanvasPanelSlot'"/Game/UI/GettingStarted.GettingStarted:WidgetTree.CanvasPanel_20.CanvasPanelSlot_0"'
End Object
Begin Object Name="CanvasPanelSlot_1" ExportPath=/Script/UMG.CanvasPanelSlot'"/Game/UI/GettingStarted.GettingStarted:WidgetTree.CanvasPanel_20.CanvasPanelSlot_1"'
LayoutData=(Offsets=(Left=309.186951,Top=39.039036,Right=627.271667,Bottom=263.185425),Anchors=(Minimum=(X=0.500000,Y=0.018519),Maximum=(X=0.500000,Y=0.018519)),Alignment=(X=0.500000,Y=0.000000))
End Object
Begin Object Name="CanvasPanelSlot_0" ExportPath=/Script/UMG.CanvasPanelSlot'"/Game/UI/GettingStarted.GettingStarted:WidgetTree.CanvasPanel_20.CanvasPanelSlot_0"'
LayoutData=(Offsets=(Left=-0.750781,Top=15.015015,Right=625.770142,Bottom=293.215454),Anchors=(Minimum=(X=0.500000,Y=0.500000),Maximum=(X=0.500000,Y=0.500000)),Alignment=(X=0.500000,Y=0.500000))
bAutoSize=True
Parent=/Script/UMG.CanvasPanel'"CanvasPanel_20"'
Content=/Script/UMG.VerticalBox'"VerticalBox_74"'
End Object
Slots(0)=/Script/UMG.CanvasPanelSlot'"CanvasPanelSlot_0"'
show all 133 lines

The UI has three input fields and three buttons that allow you to set the User ID, subscribe to a channel, and to publish a message to the same channel.

UI elements

Add logic with Blueprints

Now it's time to configure what happens when a user clicks any of the buttons. We will be using Blueprints to add PubNub logic to your application.

Set User ID

To add the logic of setting the User ID, open the Graph view and add the following nodes to your Widget Blueprint.

These nodes also print out the User ID in the output log when it's set.

Publish

Add the following nodes to your PlayerController Blueprint to add the publishing logic.

The publish functionality uses the same channel name input field used to subscribe.

Subscribe

To receive messages sent to a particular channel, you subscribe to it. When you publish a message to a channel, PubNub delivers that message to everyone subscribed to that channel.

In this app, publishing a message is triggered when you click the Publish Message button.

To subscribe, you must create the Subscribe to Channel node and bind an OnMessageReceived event to it.

For more information, refer to the Publish and Subscribe section of the SDK documentation, and to Publishing a Message.

Putting it all together

Your Blueprint should now look similar to the one below:


Now, run your app and see if you did everything correctly:

  1. Click the Set User ID button and provide a channel name in the channel name input field.

  2. Click the Subscribe button. You can see Subscribed to: {channel name} in the output log and on the screen.

  3. In the Message to publish input field, provide a message in JSON format, for example: { "text" : "This is my first realtime message!" } and click Publish Message. You can see Message published! and Message received:{ "text" : "This is my first realtime message!" } in the output log and on the screen.

Congratulations! You've just subscribed to a channel and sent your first message.

Next steps

You have just learned how to use the Unreal SDK to send and receive messages using PubNub. Next, take a look at the SDK's reference documentation which covers PubNub API in more detail.

Last updated on