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.

icon

Usage in Blueprints and C++


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

Connection status types

The connection status listener reports the following status types:

StatusDescription
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.

MyGameMode.h
#include "PubnubChat.h"

// ...other code...

UPROPERTY()
UPubnubChat* PubnubChat = nullptr;

UFUNCTION(BlueprintCallable)
void AddConnectionStatusListener();

UFUNCTION()
void OnConnectionStatusChanged(EPubnubConnectionStatus Status, const FPubnubConnectionStatusData& StatusData);

// ...other code...
MyGameMode.cpp
#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 lines

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

Output

TypeDescription
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 lines

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

Output

TypeDescription
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 lines

Troubleshooting

The recommended workflow for handling subscription errors is:

  1. Initialize connection monitoring: Add OnConnectionStatusChanged listener immediately after creating the Chat object instance.
  2. Handle connection errors: When receiving PNConnectionError status, use ReconnectSubscriptions() to restore the connection.
  3. Manual control: Use DisconnectSubscriptions() when you want to manually pause real-time updates (for example, when the app goes to background).
  4. 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.

Last updated on