Connection management
The Unreal Chat SDK provides connection management methods to handle subscription errors and restore connections when network issues occur. These methods give you full control over your chat app's connectivity and subscription state.
Connection status listener
The OnConnectionStatusChanged listener provides real-time updates about your connection status.
Use this listener to track subscription status and react to any subscription errors, such as network connectivity issues or authentication problems.
Method signature
- Blueprint
- C++ / Input parameters
1// ...other code...
2void RegisterConnectionStatusListener();
3UFUNCTION()
4void OnConnectionStatusChanged(EPubnubConnectionStatus Status, FPubnubConnectionStatusData ErrorMessage);
5// ...other code...
1#include "PubnubChatSubsystem.h"
2
3// ...other code
4void AMyGameMode::RegisterConnectionStatusListener()
5{
6	UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
7	UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
8	
9	PubnubSubsystem->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
10  // there is also a native delegate for lambda functions: FOnConnectionStatusChangedNative OnConnectionStatusChangedNative;
11}
12
13void AMyGameMode::OnConnectionStatusChanged(EPubnubConnectionStatus Status, FPubnubConnectionStatusData ErrorMessage)
14{
15	//Do something with status changed
| Parameter | Description | 
|---|---|
| StatusType:  EPubnubConnectionStatus | Enum describing the current connection status indicating whether the connection is online, offline, or has encountered an error. | 
| ErrorMessageType:  FPubnubConnectionStatusData | Object containing the error message and error code when the status is PNConnectionError. Empty for other status types. | 
Connection status types
The connection status listener reports the following status types:
| Status | Description | 
|---|---|
| PNConnectionOnline | The connection has been established and is ready to receive real-time updates. | 
| PNConnectionOffline | The connection has been intentionally terminated. | 
| PNConnectionError | The connection was unexpectedly lost or failed to establish. Contains error details. | 
Sample code
Set up a connection status listener to monitor your chat connection and automatically reconnect on errors.
1#include "PubnubChat.h"
2
3// ...other code...
4
5	UPROPERTY()
6	UPubnubChat* PubnubChat = nullptr;
7	
8	UFUNCTION(BlueprintCallable)
9	void AddConnectionStatusListener();
10	
11	UFUNCTION()
12	void OnConnectionStatusChanged(EPubnubConnectionStatus Status, const FPubnubConnectionStatusData& StatusData);
13	
14// ...other code...
1#include "Kismet/GameplayStatics.h"
2
3void AMyGameMode::AddConnectionStatusListener()
4{
5	UGameInstance* GI =  UGameplayStatics::GetGameInstance(this);
6	PubnubChatSubsystem = GI->GetSubsystem<UPubnubChatSubsystem>();
7
8	FPubnubChatConfig Config;
9	PubnubChat = PubnubChatSubsystem->InitChat("demo", "demo", "MyChatUser", Config);
10	
11	PubnubChat->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
12	
13	
14}
15
Reconnect subscriptions
The ReconnectSubscriptions() method restores the previous subscriptions with all subscribed channels and all added listeners. Call this method if DisconnectSubscriptions() was called previously or if you received a PNConnectionError from OnConnectionStatusChanged to restore all existing subscriptions and listeners.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->ReconnectSubscriptions();
Output
| Type | Description | 
|---|---|
| bool | Returns trueif the operation succeeded,falseotherwise. | 
Sample code
Reconnect all subscriptions after a connection error.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
8
9// Attempt to reconnect all subscriptions
10bool bReconnectSuccess = Chat->ReconnectSubscriptions();
11
12if (bReconnectSuccess)
13{
14    UE_LOG(LogTemp, Warning, TEXT("Successfully reconnected subscriptions"));
15}
Disconnect subscriptions
The DisconnectSubscriptions() method disconnects (pauses) all active subscriptions and listeners. Call this method when you want to pause real-time updates.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->DisconnectSubscriptions();
Output
| Type | Description | 
|---|---|
| bool | Returns trueif the operation succeeded,falseotherwise. | 
Sample code
Disconnect all subscriptions when you want to pause real-time updates.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
8
9// Disconnect all subscriptions
10bool bDisconnectSuccess = Chat->DisconnectSubscriptions();
11
12if (bDisconnectSuccess)
13{
14    UE_LOG(LogTemp, Warning, TEXT("Successfully disconnected subscriptions"));
15}
Troubleshooting
The recommended workflow for handling subscription errors is:
- Initialize connection monitoring: Add OnConnectionStatusChangedlistener immediately after creating the Chat object instance.
- Handle connection errors: When receiving PNConnectionErrorstatus, useReconnectSubscriptions()to restore the connection.
- Manual control: Use DisconnectSubscriptions()when you want to manually pause real-time updates (for example, when the app goes to background).
- Restore connection: Use ReconnectSubscriptions()to restore subscriptions when needed.
This pattern ensures your chat app maintains stable connectivity and can automatically recover from network issues without requiring users to restart the application or lose their chat state.