Initial configuration
Before building your chat app, you must initialize and configure the Unity SDK and Unity Chat SDK.
Unity SDK is a prerequisite
You must install the Unity SDK before installing the Unity Chat SDK.
Start by signing into the Admin Portal or creating an account if you don't have one yet.
Then, create an app on the Admin Portal. You will need a PubNub app to get a keyset that consists of a Subscribe Key and a Publish Key. These keys will let you establish a connection between PubNub and the chat app you're going to create with the Unity Chat SDK.
Limit of 3 keysets for Free tier accounts
Effective February 3, 2025, all Free tier accounts will be limited to a maximum of three keysets. If your account exceeds this limit, you must delete existing keysets to create new ones.
When you create a new app on the Admin Portal, the first set of demo 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.
Enable features on your keyset
Each keyset has its own configuration settings in the Admin Portal. To use some features in your chat app, you must enable appropriate settings on your app's keyset on the Admin Portal.
To use the Chat SDK, create or update users, track presence, and store messages in history, you must enable App Context, Presence, and Message Persistence.
Download the SDKs
You must install the Unity SDK before installing the Unity Chat SDK. Download the SDKs from any of the following sources:
Get the source code
You can download the source code from GitHub Unity SDK and Unity Chat SDK.
Install the SDKs via Package Manager
- Open Unity Editor and navigate to Window -> Package Manager.
- In the Package Manager window, click + and select Add package from git URL.
- Paste the PubNub Unity package links and click Add. Do it for both SDKs.
https://github.com/pubnub/unity.git?path=/PubNubUnity/Assets/PubNub
https://github.com/pubnub/unity-chat.git?path=/unity-chat/PubnubChatUnity/Assets/PubnubChat
WebGL configuration
Enable Web GL build mode only for builds
Using UnityWebGLHttpClientService
outside of WebGL builds (including the editor) might cause unexpected behavior due to UnityWebRequest
being thread-unsafe.
The PubNub Unity Chat SDK is compatible with Unity WebGL builds. To configure your project to build for WebGL:
-
Go to Edit -> Project Settings -> Player and set Managed Stripping Level to
Minimal
.Additional HTTP setup
If for some reason you can't turn on the
Secure
option in eitherPNConfiguration
or the Scriptable Object config file, then you need to also go to Project Settings -> Player -> WebGL Settings and set the Allow downloads over HTTP option to Always allowed. -
Install the WebGL Threading Patcher. Navigate to Window -> Package Manager, click +, select Add package from git URL, paste the link to the Threading Patcher's GIT repository, and click Add.
These steps configure your project for WebGL builds only. Configure other targets as needed.
Initialize PubNub
Once you have a PubNub account and an app created on the Admin Portal, you can start initializing PubNub Client API context and establish account-level credentials.
To initialize PubNub with the Unity Chat SDK, create a new Chat
object. You'll need to pass PNConfiguration
for keys and user ID, and PubnubChatConfig
for chat-specific features.
1
Initialize PubNub for WebGL
1
If you already have a PubNub instance set up for WebGL, you can pass it to the overloaded CreateInstance
method.
Input parameters
There are three overloads of CreateInstance
:
CreateInstance(PubnubChatConfig chatConfig, PNConfiguration pubnubConfig, bool webGLBuildMode = false, bool unityLogging = false)
CreateInstance(PubnubChatConfig chatConfig, PNConfigAsset configurationAsset, string userId)
CreateInstance(PubnubChatConfig chatConfig, Pubnub pubnub)
The PubnubChatConfig
parameters apply to all overloads.
PubnubChatConfig
Parameter | Feature | Description |
---|---|---|
TypingTimeout Type: int Default: 5000 | Typing Indicator | Specifies the default timeout after which the typing indicator automatically stops when no typing signals are received. The default value is set to 5000 milliseconds (5 seconds). |
TypingTimeoutDifference Type: int Default: 1000 | Typing Indicator | Specifies the difference in time between actually sending the typing indicator and the value of TypingTimeout . This is designed to cover for any network lag that may occur. The default value is set to 1000 milliseconds (1 second). |
StoreUserActivityTimestamp Type: bool Default: false | User's last online activity, global presence | Specifies if you want to track the user's global presence in your chat app. The user's activity is tracked through the LastActiveTimeStamp parameter on the User object. |
StoreUserActivityInterval Type: int Default: 60000 | User's last online activity, global presence | Specifies how often the user global presence in the app should be updated. Requires StoreUserActivityTimestamp to be set to true . The default value is set to 60000 milliseconds (1 minute). |
PNConfiguration
Parameter | Feature | Description |
---|---|---|
PublishKey *Type: string Default: n/a | Send messages | Specifies the key used to publish messages on a channel. |
SubscribeKey *Type: string Default: n/a | Receive messages | Specifies the key used to subscribe to a channel. |
UserId *Type: string Default: n/a | n/a | Unique User ID that becomes your app's current user. It's a string of up to 92 characters that identifies a single client (end user, device, or server) that connects to PubNub. Based on User ID, PubNub calculates pricing for your apps' usage. User ID should be persisted and remain unchanged. If you don't set userId , you won't be able to connect to PubNub. |
AuthKey Type: string Default: n/a | Access Manager | Authentication key for Access Manager. Use it for all requests made to PubNub APIs to authenticate users in your application and grant them access to PubNub resources (other users' metadata and channels). |
Additional options (when using PNConfiguration
)
Parameter | Description |
---|---|
webGLBuildMode Type: bool Default: false | Enables WebGL transport (sets httpTransportService to UnityWebGLHttpClientService ). |
unityLogging Type: bool Default: false | Enables Unity-specific logger (UnityPubNubLogger ). |
PNConfigAsset + userId
Parameter | Description |
---|---|
configurationAsset *Type: PNConfigAsset Default: n/a | Scriptable Object containing your PubNub keys and configuration. |
userId *Type: string Default: n/a | Client User ID used to initialize the PubNub instance created from the asset. |
Existing Pubnub
instance
Parameter | Description |
---|---|
pubnub *Type: Pubnub Default: n/a | Existing PubNub client instance to use for the chat. |
Output parameters
Type | Description |
---|---|
ChatOperationResult<Chat> | Returns a new PubNub chat instance on success or a null with error data on failure. |
Next steps
Now that you've initialized and configured the Unity Chat SDK, you can start creating channels, adding users, and powering your app with all sorts of features.