On this page

Connection management

Handle subscription errors and restore connections when network issues occur.

icon

Usage in Blueprints and C++


Connection status listener

Monitor connection status in real time to track subscription state and react to network or authentication errors.

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.

Sample code

Set up a connection status listener to monitor your chat connection and automatically reconnect on errors.

MyGameMode.h
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...
MyGameMode.cpp
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 lines

Reconnect subscriptions

Restore previous subscriptions with all subscribed channels and listeners. Call after DisconnectSubscriptions() or when receiving PNConnectionError.

Method signature

Output

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

Disconnect subscriptions

Pause all active subscriptions and listeners.

Method signature

Output

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

Troubleshooting

Recommended workflow for handling subscription errors:

  1. Initialize monitoring: Add the connection status listener immediately after creating the Chat instance.
  2. Handle errors: On PNConnectionError, call ReconnectSubscriptions().
  3. Manual control: Call DisconnectSubscriptions() when pausing real-time updates (for example, when the app backgrounds).
  4. Restore connection: Call ReconnectSubscriptions() to resume.

This pattern maintains stable connectivity and automatic recovery from network issues.

Last updated on