Connection management
Handle subscription errors and restore connections when network issues occur.
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(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
show all 16 lines| 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
show all 28 linesReconnect subscriptions
Restore previous subscriptions with all subscribed channels and listeners. Call after DisconnectSubscriptions() or when receiving PNConnectionError.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->ReconnectSubscriptions();
Output
| Type | Description |
|---|---|
bool | Returns true if the operation succeeded, false otherwise. |
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}
show all 19 linesDisconnect subscriptions
Pause all active subscriptions and listeners.
Method signature
- Blueprint
- C++ / Input parameters
1Chat->DisconnectSubscriptions();
Output
| Type | Description |
|---|---|
bool | Returns true if the operation succeeded, false otherwise. |
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}
show all 19 linesTroubleshooting
Recommended workflow for handling subscription errors:
- Initialize monitoring: Add the connection status listener immediately after creating the
Chatinstance. - Handle errors: On
PNConnectionError, 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.