Integrating Pubnub with your Unreal Project
A simple way to integrate your Pubnub app with your in-game chat
Last updated
A simple way to integrate your Pubnub app with your in-game chat
Last updated
To connect Pubnub to your in-game experience, you can make use of Pubnub's Publish, Subscribe, and History functionality to send, receive and list all messages sent to specific channels.
This can be done in both:
Your Unreal Project by making use of Pubnub's APIs.
Your web application using Pubnub's documented SDKs
For this guide, we will be focussing on setting up Pubnub to work within your Unreal project, without needing to use Unreal's Pubnub SDK. For information on setting up Pubnub in a web project, we recommend checking out Pubnub's own documentation on their chat SDKs and core SDKs, as well as how to publish and subscribe to messages via their APIs.
When creating your Pubnub application and keyset, you will find that each keyset has a Publish, Subscribe, and Secret Key associated with it. For the purposes of implementing Pubnub chat in your project, you will need to note down your Publish and Subscribe key, as these will be used as part of the API requests.
In addition to needing these keys, every request made must be associated with a Pubnub Channel. There is no special set-up required to create a new channel - they are created automatically when providing a value for channel name in any of the API calls.
We will be making use of 3 key APIs as part of integrating pubnub with the in-game chat component:
Publish API (POST):
https://ps.pndsn.com/publish/{
my-pub-key
}/{
my-sub-key
}/0/{
my-channel-name
}
/0?uuid={my-unique-id}&auth={my-auth-token}
Subscribe API (GET):
https://ps.pndsn.com/v2/subscribe/{
my-sub-key
}/{
my-channel-name
}
/0?uuid={my-unique-id}&tt={my-time-token}&tr={my-region}&auth={my-auth-token}
History API (GET):
https://ps.pndsn.com/v3/history/sub-key/{
my-sub-key
}/channel/{
my-channel-name
}
?include_message_type=true&include_uuid=true&include_meta=true&max=100&uuid={my-unique-id}&auth={my-auth-token}
There are 2 blueprints that will aid you in implementing Pubnub supported chat:
BPMC_TextChatComponent
: this is where all of your changes to integrate Pubnub will live (specifically, on MorpheusBeginPlay
).
BP_M2_PubnubConnection
: this is a helper blueprint that handles the construction of the pubnub api urls, and handles the sending of all requests. We recommend looking through this blueprint, as well as the TextChatcomponent, to get an understanding of how both blueprints work together to retrieve data from Pubnub and display it in the text chat widget.
Currently, the chat widget you see in a game is controlled by the blueprint BPMC_TextChatComponent
. You will need to make a copy of this blueprint to modify and add the below changes, as well as copy/change references in any other blueprints using this component.
To migrate to your own Pubnub application:
Go to the event graph, and on Event Morpheus Begin Play
, remove all nodes after the below comment block:
Next, add some variables (categorised under Pubnub) to the blueprint: here, we will store the Publish Key, Subscribe Key, and a Default Channel Name.
Instead of using the Social World Service (which handled retrieving the sub/pub/auth keys), we are instead going to make direct use of the Handle Fetch Chat Auth Response
function pre-provided in this component.
The example uses the Subscribe Key and Publish Key (and optionally and auth token), and passes this into our Pubnub helper blueprint BP_M2_PubnubConnection
, which is where all publish, subscribe and history requests are formatted and sent.
The channel name as mentioned above can be defined as anything you like. When initialising the Channel Name variable, ensure you give it a default value so that in cases where world/launch context id does not exist, your chat will still be sent to your default channel.
You can use any unique identifier for channel name; for example, you can use our helper node Get M2 Web Platform World ID
to retrieve the world's unique id. This will ensure that each launched world will use a separate Pubnub chat channel, so messages are properly gated.
The above method requires exposing publish and subscribe keys in editor. Whilst this won't be exposed to players, you may want to obfuscate these with a more robust handling of your pubnub keys.
A recommended approach would be to integrate the returning of keys with an existing/new web service. Pub, sub, and auth tokens can then be returns via an API call from this service, and used directly instead of the blueprint variables above. This could all be handled via an intermediary blueprint (similar to the M2 Social World Service).
If everything is set up correctly, you should be able to send messages using the chat widget in PIE as before, and the history of the chat should be shown in the in-game chat!