---
source_url: https://www.pubnub.com/docs/chat/unreal-chat-sdk/build/features/connection-management
title: Connection management
updated_at: 2026-06-12T11:23:23.066Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Connection management

Handle subscription errors and restore connections when network issues occur.

##### Usage in Blueprints and C++

You can use PubNub's functionality via Blueprints or directly in C++ code.

* In Blueprints, you can access PubNub from any Widget or Actor. Start by [initializing chat](https://www.pubnub.com/docs/chat/unreal-chat-sdk/build/configuration#initialize-pubnub-chat) using `InitChat` on `UPubnubChatSubsystem`. Afterwards, you can use the returned `UPubnubChat` object reference to call all Chat SDK functions.

:::warning Blueprint functions
The Blueprints provided in the documentation show how you can structure your application and may contain utility methods and elements like buttons that are not part of the Unreal Chat SDK and are meant to serve as guidance.
:::

* In C++, you can use UPubnubChatSubsystem as any other Game Instance Subsystem. #include "Kismet/GameplayStatics.h" #include "Engine/GameInstance.h" #include "PubnubChatSubsystem.h" // ACTION REQUIRED: Replace ASample_ChatSubsystem with name of your Actor class void ASample_ChatSubsystem::InitChatSample() { // Get PubnubChatSubsystem from GameInstance UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this); UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>(); // Initialize Chat - InitChat may fail under some conditions so make sure to check the Result for errors before using the Chat FPubnubChatInitChatResult InitChatResult = PubnubChatSubsystem->InitChat(TEXT("demo"), TEXT("demo"), TEXT("Player_001")); UPubnubChat* PubnubChat = InitChatResult.Chat; } Chat now allows you to call all Chat SDK functions, for example, Chat->GetChannel("my_channel").

:::note Asynchronous and synchronous method execution
Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.
* Asynchronous methods (Async suffix) return void and take an optional delegate parameter that fires when the operation completes. 1Chat->ReconnectSubscriptionsAsync(OnReconnectSubscriptionsResponseDelegate); You can also use native callbacks that accept lambdas instead of dynamic delegates. Native callback types have the Native suffix (for example, FOnPubnubChatOperationResponseNative).
* Synchronous methods (no suffix) block the main game thread until the operation completes and return a result struct directly. 1FPubnubChatOperationResult Result = Chat->ReconnectSubscriptions();
:::

## 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

GameMode.h

```cpp
// ...other code...
void RegisterConnectionStatusListener();
UFUNCTION()
void OnConnectionStatusChanged(EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData);
// ...other code...
```

GameMode.cpp

```cpp
#include "PubnubChatSubsystem.h"

// ...other code
void AMyGameMode::RegisterConnectionStatusListener()
{
	UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
	UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

	FPubnubChatInitChatResult InitResult = PubnubChatSubsystem->InitChat("demo", "demo", "MyChatUser");
	UPubnubChat* Chat = InitResult.Chat;

	Chat->OnConnectionStatusChanged.AddDynamic(this, &AMyGameMode::OnConnectionStatusChanged);
	// there is also a native delegate for lambda functions: FOnPubnubChatConnectionStatusChangedNative OnConnectionStatusChangedNative;
}

void AMyGameMode::OnConnectionStatusChanged(EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData)
{
	// Do something with status changed
}
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| Status | EPubnubChatConnectionStatus | Optional |  | Enum describing the current connection status indicating whether the connection is online, offline, or has encountered an error. |
| StatusData | FPubnubChatConnectionStatusData | Optional |  | Object containing the error message and error code when the status is `PCCS_ConnectionError`. Empty for other status types. |

### Connection status types

The connection status listener reports the following status types:

| Status | Description |
| --- | --- |
| `PCCS_ConnectionOnline` | The connection has been established and is ready to receive real-time updates. |
| `PCCS_ConnectionOffline` | The connection has been intentionally terminated. |
| `PCCS_ConnectionError` | The connection was unexpectedly lost or failed to establish. Contains error details. |

### Sample code

:::tip Reference code
This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with `ACTION REQUIRED` before running the code. Use it as a reference when working with other examples in this document.
:::

Set up a connection status listener to monitor connection changes.

###### C++

###### Actor.h

```cpp
// blueprint.a9-bs_-x
UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|Chat|Connection Status")
void ConnectionStatusChangedSample();

void OnConnectionStatusChanged(EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData);
```

###### Actor.cpp

```cpp
// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::ConnectionStatusChangedSample()
{
	// snippet.hide
	UPubnubChat* Chat = nullptr;
	// snippet.show

	// Assumes Chat is a valid and initialized instance of UPubnubChat

	// Bind to connection status changes (e.g. to update UI or log state)
	Chat->OnConnectionStatusChangedNative.AddUObject(this, &ASample_Chat::OnConnectionStatusChanged);
}

// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::OnConnectionStatusChanged(EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData)
{
	//React to connection statuses, for example check for errors
	if (Status == EPubnubChatConnectionStatus::PCCS_ConnectionError)
	{
		UE_LOG(LogTemp, Error, TEXT("Connection Error. Reason: %s"), *StatusData.Reason);
	}
}
```

###### Blueprint

## Reconnect subscriptions

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

### Method signature

```cpp
Chat->ReconnectSubscriptions(FString Timetoken = "");
```

| Parameter | Description |
| --- | --- |
| `Timetoken`Type: `FString`Default: `""` | Optional timetoken to resume subscriptions from a specific point. If empty, resumes from the current time. |

#### Output

| Type | Description |
| --- | --- |
| `FPubnubChatOperationResult` | Operation result with `Error` (bool), `ErrorMessage` (FString), and `StepResults` (per-step details). |

### Sample code

Reconnect all subscriptions after a connection error.

```cpp
// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::ReconnectSubscriptionsSample()
{
	// snippet.hide
	UPubnubChat* Chat = nullptr;
	// snippet.show

	// Assumes Chat is a valid and initialized instance of UPubnubChat

	// Reconnect all active subscriptions (e.g. after disconnect or connection error from OnConnectionStatusChanged)
	FOnPubnubChatOperationResponseNative Callback;
	// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
	Callback.BindUObject(this, &ASample_Chat::OnReconnectSubscriptionsResponse);
	Chat->ReconnectSubscriptionsAsync(Callback);

	// Optional: pass a timetoken to resume from a specific point
	// Chat->ReconnectSubscriptionsAsync(Callback, Timetoken);
}

// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::OnReconnectSubscriptionsResponse(const FPubnubChatOperationResult& Result)
{
	if (Result.Error) { return; }
	// Reconnect completed
}
```

### Other examples

#### Reconnect with connection status listener

###### Actor.h

```cpp
UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|Chat|Connection Status")
void ConnectionStatusChangedReconnectSample();
```

###### Actor.cpp

```cpp
// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::ConnectionStatusChangedReconnectSample()
{
	// snippet.hide
	UPubnubChat* Chat = nullptr;
	// snippet.show

	// Assumes Chat is a valid and initialized instance of UPubnubChat

	Chat->OnConnectionStatusChangedNative.AddLambda([this, Chat](https://www.pubnub.com/docs/EPubnubChatConnectionStatus Status, const FPubnubChatConnectionStatusData& StatusData)
	{
		if (!Chat)
		{ return; }
		
		// Try to reconnect subscriptions when there is a connection error
		if (Status == EPubnubChatConnectionStatus::PCCS_ConnectionError)
		{
			Chat->ReconnectSubscriptions();
		}
	});
}
```

## Disconnect subscriptions

Pause all active subscriptions and listeners.

### Method signature

```cpp
Chat->DisconnectSubscriptions();
```

#### Output

| Type | Description |
| --- | --- |
| `FPubnubChatOperationResult` | Operation result with `Error` (bool), `ErrorMessage` (FString), and `StepResults` (per-step details). |

### Sample code

Disconnect all subscriptions when you want to pause real-time updates.

```cpp
// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::DisconnectSubscriptionsSample()
{
	// snippet.hide
	UPubnubChat* Chat = nullptr;
	// snippet.show

	// Assumes Chat is a valid and initialized instance of UPubnubChat

	// Pause all active subscriptions (e.g. when going to background or before ReconnectSubscriptions)
	FOnPubnubChatOperationResponseNative Callback;
	// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
	Callback.BindUObject(this, &ASample_Chat::OnDisconnectSubscriptionsResponse);
	Chat->DisconnectSubscriptionsAsync(Callback);
}

// ACTION REQUIRED: Replace ASample_Chat with name of your Actor class
void ASample_Chat::OnDisconnectSubscriptionsResponse(const FPubnubChatOperationResult& Result)
{
	if (Result.Error) { return; }
	// Disconnect completed; call ReconnectSubscriptions when ready to resume
}
```

## Troubleshooting

Recommended workflow for handling subscription errors:

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

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