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
- 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.
- 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 ChannelName,
FString Message,
FPubnubPublishSettings PublishSettings = FPubnubPublishSettings()
);
Parameter | Type | Required | Description |
---|---|---|---|
Message | FString | Yes | The payload. |
ChannelName | 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. |
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 ChannelName = "randomChannel";
FString Message = "{ \"text\" : \"This is my message\" }";
// Publish the message using default publish settings
PubnubSubsystem->PublishMessage(ChannelName, Message);
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 ChannelName = "randomChannel";
FString Message = "{ \"text\" : \"This is my message\" }";
// Create the publish settings
FPubnubPublishSettings PublishSettings;
PublishSettings.PublishMethod = EPubnubPublishMethod::pubnubSendViaPOSTwithGZIP;
// Publish the message using the specified publish settings
PubnubSubsystem->PublishMessage(ChannelName, 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 ChannelName,
FString Message
);
Parameter | Type | Required | Description |
---|---|---|---|
Message | FString | Yes | The payload. |
ChannelName | FString | Yes | The channel to sent the signal to. |
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 ChannelName = "randomChannel";
FString Message = "{ \"text\" : \"This is my signal\" }";
// Send the signal
PubnubSubsystem->Signal(ChannelName, Message);
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)
- Blueprint
- C++
// subscribe to a channel
PubnubSubsystem->SubscribeToChannel(FString ChannelName);
// subscribe to a channel group
PubnubSubsystem->SubscribeToGroup(FString GroupName);
Parameter | Type | Required | Description |
---|---|---|---|
ChannelName | 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 ChannelName = "randomChannel";
// Subscribe
PubnubSubsystem->Subscribe(ChannelName);
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 ChannelName = "randomChannel";
FString PresenceChannel = ChannelName + "-pnpres";
// Subscribe
PubnubSubsystem->Subscribe(ChannelName);
// 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 ChannelName = "foo.*";
// Subscribe
PubnubSubsystem->Subscribe(ChannelName);
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 ChannelName);
// unsubscribe from a channel group
PubnubSubsystem->UnsubscribeFromGroup(FString GroupName);
Parameter | Type | Required | Description |
---|---|---|---|
ChannelName | 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 ChannelName = "randomChannel";
FString ChannelGroupName = "randomChannelGroup";
// Subscribe
PubnubSubsystem->Subscribe(ChannelName);
PubnubSubsystem->SubscribeToGroup(ChannelGroupName);
// Unsubscribe
PubnubSubsystem->UnsubscribeFromChannel(ChannelName);
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 ChannelName = "randomChannel";
FString ChannelGroupName = "randomChannelGroup";
// Subscribe
PubnubSubsystem->Subscribe(ChannelName);
PubnubSubsystem->SubscribeToGroup(ChannelGroupName);
// Unsubscribe all
PubnubSubsystem->UnsubscribeFromAll();
Returns
This method doesn't have a return value.