Publish/Subscribe API for PubNub Unreal SDK

The foundation of the PubNub service is the ability to send a message and have it delivered anywhere in less than 100ms. Send a message to just one other person, or broadcast to thousands of subscribers at once.

For higher-level conceptual details on publishing and subscribing, refer to Connection Management and to Publish Messages.

icon

Usage in Blueprints and C++

Publish

publish() sends a message to all channel subscribers. A successfully published message is replicated across PubNub's points of presence and sent simultaneously to all subscribed clients on a channel.

  • You must initialize PubNub with the publishKey.
  • You don't have to be subscribed to a channel to publish to it.
  • You cannot publish to multiple channels simultaneously.

Method(s)

Basic Usage

Publish a message to a channel

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString Message = "{ \"text\" : \"This is my message\" }";

// Create an instance of FPubnubPublishSettings
FPubnubPublishSettings PublishSettings;
PublishSettings.CustomMessageType = "text-message"; // Set the CustomMessageType

// Publish the message with custom publish settings
PubnubSubsystem->PublishMessage(ChannelName, Message, PublishSettings);
show all 16 lines
Subscribe to the channel

Before running the above publish example (either using the Debug Console or in a separate script running in a new terminal window), subscribe to the same channel that you publish the message to.

Returns

This method doesn't have a return value.

Other Examples

Publish a message using POST with GZIP

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString Message = "{ \"text\" : \"This is my message\" }";

// Create the publish settings
FPubnubPublishSettings PublishSettings;
PublishSettings.PublishMethod = EPubnubPublishMethod::PPM_SendViaPOSTwithGZIP;

// Publish the message using the specified publish settings
PubnubSubsystem->PublishMessage(Channel, Message, PublishSettings);

Signal

The signal() function is used to send a signal to all subscribers of a channel.

By default, signals are limited to a message payload size of 64 bytes. This limit applies only to the payload, and not to the URI or headers. If you require a larger payload size, contact support.

Method(s)

Basic Usage

Signal a message to a channel

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString Message = "{ \"text\" : \"This is my signal\" }";

// Create the signal settings
FPubnubSignalSettings SignalSettings;
PublishSettings.CustomMessageType = "text-message";

// Send the signal
PubnubSubsystem->Signal(Channel, Message, SignalSettings);
show all 16 lines

Returns

This method doesn't have a return value.

Subscribe

Receive messages

Your app receives messages and events via event listeners. The event listener is a single point through which your app receives all the messages, signals, and events that are sent in any channel you are subscribed to.

For more information about adding a listener, refer to the Event Listeners section.

Description

This function causes the client to create an open TCP socket to the PubNub Real-Time Network and begin listening for messages on a specified channel. To subscribe to a channel, the client must send the appropriate SubscribeKey at initialization.

By default, a newly subscribed client will only receive messages published to the channel after the Subscribe() call completes.

Unsubscribing from all channels

Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken and thus, there could be some gaps in the subscription that may lead to message loss.

Method(s)

Basic Usage

Subscribe to a channel:

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";

// Subscribe
PubnubSubsystem->Subscribe(Channel);
Event listeners

The response of the call is handled by adding a listener. Refer to the Event Listeners section for more details. Listeners should be added before calling the method.

Returns

This method doesn't have a return value. To receive messages, you must add a listener.

Other Examples

Subscribing with presence

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

For any given channel there is an associated Presence channel. You can subscribe directly to the channel by appending -pnpres to the channel name. For example, the channel named my_channel would have the presence channel named my_channel-pnpres.

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString PresenceChannel = Channel + "-pnpres";

// Subscribe
PubnubSubsystem->Subscribe(Channel);
// Subscribe to the Presence channel
PubnubSubsystem->Subscribe(PresenceChannel);
Response

All presence events are received via a listener. For more information on the structure of the received events, refer to Event types.

Wildcard subscribe to channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal (with Enable Wildcard Subscribe checked). Read the support page on enabling add-on features on your keys.

Wildcard subscribes allow the client to subscribe to multiple channels using wildcard. For example, if you subscribe to a.* you will get all messages for a.b, a.c, a.x. The wildcarded * portion refers to any portion of the channel string name after the dot (.).

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "foo.*";

// Subscribe
PubnubSubsystem->Subscribe(Channel);
Wildcard grants and revokes

Only one level (a.*) of wildcarding is supported. If you grant on * or a.b.*, the grant will treat * or a.b.* as a single channel named either * or a.b.*. You can also revoke permissions from multiple channels using wildcards but only if you previously granted permissions using the same wildcards. Wildcard revokes, similarly to grants, only work one level deep, like a.*.

Unsubscribe

When subscribed to a single channel, this function causes the client to issue a leave from the channel and close any open socket to the PubNub Network. For multiplexed channels, the specified channel(s) will be removed and the socket remains open until there are no more channels remaining in the list.

Unsubscribing from all channels

Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken and thus, there could be some gaps in the subscription that may lead to message loss.

Method(s)

Basic Usage

Unsubscribe from a channel:

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString ChannelGroupName = "randomChannelGroup";

// Subscribe
PubnubSubsystem->Subscribe(Channel);
PubnubSubsystem->SubscribeToGroup(ChannelGroupName);

// Unsubscribe
PubnubSubsystem->UnsubscribeFromChannel(Channel);
show all 16 lines

Returns

This method doesn't have a return value.

Unsubscribe All

Unsubscribe from all channels and all channel groups.

Method(s)

Basic Usage

#include "Kismet/GameplayStatics.h"
#include "PubnubSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();

FString Channel = "randomChannel";
FString ChannelGroupName = "randomChannelGroup";

// Subscribe
PubnubSubsystem->Subscribe(Channel);
PubnubSubsystem->SubscribeToGroup(ChannelGroupName);

// Unsubscribe all
PubnubSubsystem->UnsubscribeFromAll();

Returns

This method doesn't have a return value.

Last updated on