Connection management
Handle subscription errors and restore connections when network issues occur.
Asynchronous and synchronous method execution
Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.
-
Asynchronous methods (
Asyncsuffix) returnvoidand take an optional delegate parameter that fires when the operation completes.1Chat->ReconnectSubscriptionsAsync(OnReconnectSubscriptionsResponseDelegate);You can also use native callbacks that accept lambdas instead of dynamic delegates. Native callback types have the
Nativesuffix (for example,FOnPubnubChatOperationResponseNative). -
Synchronous methods (no suffix) block the main game thread until the operation completes and return a result struct directly.
1FPubnubChatOperationResult Result = Chat->ReconnectSubscriptions();
Connection status listener
Monitor connection status in real time to track subscription state and react to network or authentication errors.
Method signature
- Blueprint
- C++ / Input parameters
1// ...other code...
2void RegisterConnectionStatusListener();
3UFUNCTION()
4void OnConnectionStatusChanged(EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData);
5// ...other code...
1#include "PubnubChatSubsystem.h"
2
3// ...other code
4void AMyGameMode::RegisterConnectionStatusListener()
5{
6 UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
7 UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
8
9 FPubnubChatInitChatResult InitResult = PubnubChatSubsystem->InitChat("demo", "demo", "MyChatUser");
10 UPubnubChat* Chat = InitResult.Chat;
11
12 Chat->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
13 // there is also a native delegate for lambda functions: FOnPubnubChatConnectionStatusChangedNative OnConnectionStatusChangedNative;
14}
15
show all 19 lines| Parameter | Description |
|---|---|
StatusType: EPubnubChatConnectionStatus | Enum describing the current connection status indicating whether the connection is online, offline, or has encountered an error. |
StatusDataType: FPubnubChatConnectionStatusData | Object containing the error message and error code when the status is PCCS_ConnectionError. Empty for other status types. |
Connection status types
The connection status listener reports the following status types:
| Status | Description |
|---|---|
PCCS_ConnectionOnline | The connection has been established and is ready to receive real-time updates. |
PCCS_ConnectionOffline | The connection has been intentionally terminated. |
PCCS_ConnectionError | The connection was unexpectedly lost or failed to establish. Contains error details. |
Sample code
Reference code
This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.
Set up a connection status listener to monitor connection changes.
Reconnect subscriptions
Restore previous subscriptions with all subscribed channels and listeners. Call after DisconnectSubscriptions() or when receiving PCCS_ConnectionError.
Method signature
1Chat->ReconnectSubscriptions(FString Timetoken = "");
| Parameter | Description |
|---|---|
TimetokenType: FStringDefault: "" | Optional timetoken to resume subscriptions from a specific point. If empty, resumes from the current time. |
Output
| Type | Description |
|---|---|
FPubnubChatOperationResult | Operation result with Error (bool), ErrorMessage (FString), and StepResults (per-step details). |
Sample code
Reconnect all subscriptions after a connection error.
1
Other examples
Reconnect with connection status listener
Actor.h
1
Actor.cpp
1
Disconnect subscriptions
Pause all active subscriptions and listeners.
Method signature
1Chat->DisconnectSubscriptions();
Output
| Type | Description |
|---|---|
FPubnubChatOperationResult | Operation result with Error (bool), ErrorMessage (FString), and StepResults (per-step details). |
Sample code
Disconnect all subscriptions when you want to pause real-time updates.
1
Troubleshooting
Recommended workflow for handling subscription errors:
- Initialize monitoring: Add the connection status listener immediately after creating the
Chatinstance. - Handle errors: On
PCCS_ConnectionError, callReconnectSubscriptions(). - Manual control: Call
DisconnectSubscriptions()when pausing real-time updates (for example, when the app backgrounds). - Restore connection: Call
ReconnectSubscriptions()to resume.
This pattern maintains stable connectivity and automatic recovery from network issues.