Initial configuration
Initialize and configure the Unity SDK and Unity Chat SDK before building your chat app.
Unity SDK is a prerequisite
You must install the Unity SDK before installing the Unity Chat SDK.
Prerequisites
- Sign in or create an account on the Admin Portal.
- Create an app to get your Publish Key and Subscribe Key.
A new app receives demo keys automatically. You can create multiple keysets per app. Use separate keysets for production and test environments.
Required keyset settings
Enable these features on your keyset in the Admin Portal:
- App Context - Store user and channel data
- Presence - Track online/offline status
- Message Persistence - Store message history
Download the SDKs
Install the Unity SDK before installing the Unity Chat SDK.
Source code
Download the source code from GitHub: Unity SDK and Unity Chat SDK.
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 WebGL 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 Unity Chat SDK is compatible with WebGL builds. Configure your project 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
Secureoption in eitherPNConfigurationor 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
Use the CreateInstance() method to create a Chat SDK instance. 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.
Method signatures
Three overloads of CreateInstance are available:
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
TypingTimeout#- Specifies the default timeout after which the typing indicator automatically stops when no typing signals are received. The default value is set to
5000milliseconds (5 seconds).- Feature
- Typing Indicator
TypingTimeoutDifference#- 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 to1000milliseconds (1 second).- Feature
- Typing Indicator
StoreUserActivityTimestamp#- Specifies if you want to track the user's global presence in your chat app. The user's activity is tracked through the
LastActiveTimeStampparameter on theUserobject. StoreUserActivityInterval#- Specifies how often the user global presence in the app should be updated. Requires
StoreUserActivityTimestampto be set totrue. The default value is set to60000milliseconds (1 minute). SyncMutedUsers#- Specifies if you want to persist the mute list for regular chat users beyond the scope of a single session. When set to
true, the mute list is saved and retrieved during client initialization.- Feature
- Moderation as user
EmitReadReceiptEvents#- Controls whether read receipt events are emitted per channel type. Keys are channel type strings (
"public","group","direct"), values are booleans. By default, read receipts are enabled for group and direct channels but disabled for public channels.- Feature
- Read Receipts
RateLimitFactor#- Exponential backoff factor used by the rate limiter when a channel's send limit is exceeded. A value of
2means each retry waits twice as long as the previous attempt.- Feature
- Send messages
RateLimitsPerChannel#- Per-channel-type rate limit in milliseconds applied between
SendText()calls. Set to0to disable rate limiting for that channel type. See below for theRateLimitPerChannelfields.- Feature
- Send messages
PushNotifications#- Configuration for mobile push notifications. See below for
PushNotificationsConfigoptions.- Feature
- Mobile Push Notifications
RateLimitPerChannel
Configure per-channel-type rate limiting by setting the RateLimitsPerChannel property of PubnubChatConfig:
DirectConversation#- Minimum milliseconds between
SendText()calls ondirectchannels.0disables rate limiting. GroupConversation#- Minimum milliseconds between
SendText()calls ongroupchannels.0disables rate limiting. PublicConversation#- Minimum milliseconds between
SendText()calls onpublicchannels.0disables rate limiting. UnknownConversation#- Minimum milliseconds between
SendText()calls on channels with an unrecognized type.0disables rate limiting.
PushNotificationsConfig
Configure mobile push notifications by setting the PushNotifications property of PubnubChatConfig:
SendPushes#- Specifies whether push notifications are enabled for sending messages. When
true, the SDK automatically adds push notification payloads to messages sent on registered channels. DeviceToken#- The unique device token obtained from the push notification service (APNs or FCM). This identifies the device that will receive push notifications.
DeviceGateway#- The push notification gateway to use. Options include
PNPushType.FCMfor Firebase Cloud Messaging (Android) orPNPushType.APNS2for Apple Push Notification service (iOS). APNSTopic#- The bundle identifier for your iOS app. Required when using APNs.
APNSEnvironment#- The APNs environment to use. Options are
PushEnvironment.DevelopmentorPushEnvironment.Production.
PNConfiguration
PublishKeyrequired#- Specifies the key used to publish messages on a channel.
- Feature
- Send messages
SubscribeKeyrequired#- Specifies the key used to subscribe to a channel.
- Feature
- Receive messages
UserIdrequired#- 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.- Feature
- n/a
AuthKey#- 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).
- Feature
- Access Manager
Additional options (when using PNConfiguration)
PNConfigAsset + userId
Existing Pubnub instance
pubnubrequired#- 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
After initialization, you can:
- Create channels and users
- Add features like messaging, typing indicators, and presence