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.
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.
- Prerequisites and limitations
- Message data
- Size
- Publish rate
- Custom message type
- Best practices
- 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.
The message can contain any JSON-serializable data (Objects, Arrays, Ints, Strings) and shouldn't contain any special classes or functions. String content can include any single-byte or multi-byte UTF-8 characters.
Don't JSON serialize
You should not JSON serialize the message
and meta
parameters when sending signals, messages, or files as the serialization is automatic. Pass the full object as the message/meta payload and let PubNub handle everything.
The maximum message size is 32 KiB, including the final escaped character count and the channel name. An optimal message size is under 1800 bytes.
If the message you publish exceeds the configured size, you receive a Message Too Large
error. If you want to learn more or calculate your payload size, refer to Message Size Limit.
You can publish as fast as bandwidth conditions allow. There is a soft limit based on max throughput since messages will be discarded if the subscriber can't keep pace with the publisher.
For example, if 200 messages are published simultaneously before a subscriber has had a chance to receive any, the subscriber may not receive the first 100 messages because the message queue has a limit of only 100 messages stored in memory.
You can optionally provide the CustomMessageType
parameter to add your business-specific label or category to the message, for example text
, action
, or poll
.
- Publish to any given channel in a serial manner (not concurrently).
- Check that the return code is success (for example,
[1,"Sent","136074940..."]
) - Publish the next message only after receiving a success return code.
- If a failure code is returned (
[0,"blah","<timetoken>"]
), retry the publish. - Avoid exceeding the in-memory queue's capacity of 100 messages. An overflow situation (aka missed messages) can occur if slow subscribers fail to keep up with the publish pace in a given period of time.
- Throttle publish bursts according to your app's latency needs, for example no more than 5 messages per second.
Method(s)
- Blueprint
- C++
PubnubSubsystem->PublishMessage(
FString Channel,
FString Message,
FPubnubPublishSettings PublishSettings = FPubnubPublishSettings()
);
Parameter | Type | Required | Description |
---|---|---|---|
Message | FString | Yes | The payload. |
Channel | FString | Yes | The channel to sent the message to. |
PublishSettings | FPubnubPublishSettings | Optional | Struct defining publish configuration. |
FPubnubPublishSettings
Parameter | Type | Required | Description |
---|---|---|---|
StoreInHistory | bool | Optional | Whether to store the message so it is available to be returned by the History API. true by default. |
MetaData | FString | Optional | A JSON object containing additional (meta) data about the message which you can use with the filtering ability. |
PublishMethod | EPubnubPublishMethod | Optional | Which HTTP method to use for the publish transaction. Available values:
|
Replicate | bool | Optional | If true, the message is replicated and is received by all subscribers. If false, the message is not replicated and is delivered only to Functions event handlers. |
CustomMessageType | FString | Optional | A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn- . Examples: text , action , poll . |
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 linesSubscribe 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)
- Blueprint
- C++
PubnubSubsystem->Signal(
FString Channel,
FString Message,
FPubnubSignalSettings SignalSettings = FPubnubSignalSettings()
);
Parameter | Type | Required | Description |
---|---|---|---|
Message | FString | Yes | The payload. |
Channel | FString | Yes | The channel to sent the signal to. |
SignalSettings | FPubnubSignalSettings | Optional | Struct defining signal configuration. |
FPubnubSignalSettings
Parameter | Type | Required | Description |
---|---|---|---|
CustomMessageType | FString | Optional | A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn- . Examples: text , action , poll . |
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 linesReturns
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)
- Blueprint
- C++
// subscribe to a channel
PubnubSubsystem->SubscribeToChannel(FString Channel);
// subscribe to a channel group
PubnubSubsystem->SubscribeToGroup(FString GroupName);
Parameter | Type | Required | Description |
---|---|---|---|
Channel | FString | Yes | The channel to subscribe to. |
GroupName | FString | Yes | The channel group to subscribe to. |
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)
- Blueprint
- C++
// unsubscribe from a channel
PubnubSubsystem->UnsubscribeFromChannel(FString Channel);
// unsubscribe from a channel group
PubnubSubsystem->UnsubscribeFromGroup(FString GroupName);
Parameter | Type | Required | Description |
---|---|---|---|
Channel | FString | Yes | The channel to unsubscribe from. |
GroupName | FString | Yes | The channel group to unsubscribe from. |
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 linesReturns
This method doesn't have a return value.
Unsubscribe All
Unsubscribe from all channels and all channel groups.
Method(s)
- Blueprint
- C++
PubnubSubsystem->UnsubscribeFromAll();
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.