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
// ...other code...
void RegisterConnectionStatusListener();
UFUNCTION()
void OnConnectionStatusChanged(EPubnubConnectionStatus Status, FPubnubConnectionStatusData ErrorMessage);
// ...other code...
#include "PubnubChatSubsystem.h"
// ...other code
void AMyGameMode::RegisterConnectionStatusListener()
{
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
PubnubSubsystem->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
// there is also a native delegate for lambda functions: FOnConnectionStatusChangedNative OnConnectionStatusChangedNative;
}
void AMyGameMode::OnConnectionStatusChanged(EPubnubConnectionStatus Status, FPubnubConnectionStatusData ErrorMessage)
{
//Do something with status changed
show all 16 linesParameter | Description |
---|---|
Status Type: EPubnubConnectionStatus | Enum describing the current connection status indicating whether the connection is online, offline, or has encountered an error. |
ErrorMessage Type: 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. |
Basic usage
Set up a connection status listener to monitor your chat connection and automatically reconnect on errors.
#include "PubnubChat.h"
// ...other code...
UPROPERTY()
UPubnubChat* PubnubChat = nullptr;
UFUNCTION(BlueprintCallable)
void AddConnectionStatusListener();
UFUNCTION()
void OnConnectionStatusChanged(EPubnubConnectionStatus Status, const FPubnubConnectionStatusData& StatusData);
// ...other code...
#include "Kismet/GameplayStatics.h"
void AMyGameMode::AddConnectionStatusListener()
{
UGameInstance* GI = UGameplayStatics::GetGameInstance(this);
PubnubChatSubsystem = GI->GetSubsystem<UPubnubChatSubsystem>();
FPubnubChatConfig Config;
PubnubChat = PubnubChatSubsystem->InitChat("demo", "demo", "MyChatUser", Config);
PubnubChat->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
}
show all 28 linesReconnect 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
Chat->ReconnectSubscriptions();
Output
Type | Description |
---|---|
bool | Returns true if the operation succeeded, false otherwise. |
Basic usage
Reconnect all subscriptions after a connection error.
#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
UPubnubChat* Chat = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
// Attempt to reconnect all subscriptions
bool bReconnectSuccess = Chat->ReconnectSubscriptions();
if (bReconnectSuccess)
{
UE_LOG(LogTemp, Warning, TEXT("Successfully reconnected subscriptions"));
}
show all 19 linesDisconnect 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
Chat->DisconnectSubscriptions();
Output
Type | Description |
---|---|
bool | Returns true if the operation succeeded, false otherwise. |
Basic usage
Disconnect all subscriptions when you want to pause real-time updates.
#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
UPubnubChat* Chat = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
// Disconnect all subscriptions
bool bDisconnectSuccess = Chat->DisconnectSubscriptions();
if (bDisconnectSuccess)
{
UE_LOG(LogTemp, Warning, TEXT("Successfully disconnected subscriptions"));
}
show all 19 linesTroubleshooting
The recommended workflow for handling subscription errors is:
- Initialize connection monitoring: Add
OnConnectionStatusChanged
listener immediately after creating the Chat object instance. - Handle connection errors: When receiving
PNConnectionError
status, 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.