Publish/Subscribe API for Unreal SDK

icon

Usage in Blueprints and C++

PubNub delivers messages worldwide in less than 30 ms. Send a message to one recipient or broadcast to thousands of subscribers.

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. PubNub replicates the message across its points of presence and delivers it to all subscribed clients on that 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)

PubnubSubsystem->PublishMessage(
FString Channel,
FString Message,
FOnPublishMessageResponse OnPublishMessageResponse,
FPubnubPublishSettings PublishSettings = FPubnubPublishSettings()
);
* required
ParameterDescription
Message *
Type: FString
The message to publish. Can be a literal string or JSON-formatted string containing serialized data.
Channel *
Type: FString
The channel ID to send the message to.
PublishSettingsStruct defining publish configuration.
OnPublishMessageResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnPublishMessageResponseNative to handle the result using a lambda.

FPubnubPublishSettings


* required
ParameterDescription
StoreInHistory
Type: bool
Whether to store the message so it is available to be returned by the History API. true by default.
Ttl
Type: int
Time-to-live (TTL) for the message in hours. If not specified, the message will use the default retention period configured for the key.
MetaData
Type: FString
A JSON object containing additional (meta) data about the message which you can use with the filtering ability.
PublishMethod
Type: EPubnubPublishMethod
Which HTTP method to use for the publish transaction. Available values:

  • PPM_SendViaGET
  • PPM_SendViaPOST
  • PPM_UsePATCH
  • PPM_SendViaPOSTwithGZIP
  • PPM_UsePATCHwithGZIP
  • PPM_UseDELETE
Replicate
Type: bool
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
Type: FString
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.

Sample code

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Publish a message to a channel

Actor.h



Actor.cpp



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 function is void, but the delegate returns the FOnPublishMessageResponse struct.

FOnPublishMessageResponse

FieldTypeDescription
Result
FPubnubOperationResult
The result of the operation.
PublishedMessage
FPubnubMessageData
A struct containing the result of the operation.

FPubnubMessageData

FieldTypeDescription
Message
FString
The message itself.
Channel
FString
Channel that the message was published to.
UserID
FString
The message information about the publisher.
Timetoken
FString
The time token of the message, indicating when it was published.
Metadata
FString
The message metadata, as published.
MessageType
EPubnubMessageType
Indicates the message type: a signal, published, or another type.
CustomMessageType
FString
User-provided message type.
MatchOrGroup
FString
Subscription match or the channel group.
region
int
Region of the message. Not relevant in most cases.
flags
int
Message flags.

EPubnubMessageType

FieldDescription
PMT_Signal
Indicates that the message was received as a signal.
PMT_Published
Indicates that the message was published.
PMT_Action
Indicates an action on a published message.
PMT_Objects
Message about Objects.
PMT_Files
Message about Files.

FOnPublishMessageResponseNative

FieldTypeDescription
Result
const FPubnubOperationResult&
The result of the operation.
PublishedMessage
const FPubnubMessageData&
A struct containing the result of the operation.

Other examples

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Publish a message with TTL

Actor.h


Actor.cpp



Publish a message with metadata

Actor.h


Actor.cpp



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)

PubnubSubsystem->Signal(
FString Channel,
FString Message,
FOnSignalResponse OnSignalResponse,
FPubnubSignalSettings SignalSettings = FPubnubSignalSettings()
);
* required
ParameterDescription
Channel *
Type: FString
The channel ID to send the signal to.
Message *
Type: FString
The message to publish. Can be a literal string or JSON-formatted string containing serialized data.
OnSignalResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSignalResponseNative to handle the result using a lambda.
SignalSettingsStruct defining signal configuration.

FPubnubSignalSettings


* required
ParameterDescription
CustomMessageType
Type: FString
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.

Sample code

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Signal a message to a channel

Actor.h



Actor.cpp



Returns

This function is void, but the delegate returns the FOnSignalResponse struct.

FOnSignalResponse

FieldTypeDescription
Result
FPubnubOperationResult
The result of the operation.
SignalMessage
FPubnubMessageData
A struct containing the result of the operation.

FOnSignalResponseNative

FieldTypeDescription
Result
const FPubnubOperationResult&
The result of the operation.
SignalMessage
const FPubnubMessageData&
A struct containing the result of the operation.

Other examples

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Signal with custom message type

Actor.h


Actor.cpp



Subscribe

Receive messages

Your app receives messages and events via event listeners. The OnMessageReceived 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 ID. To subscribe to a channel ID, 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)

// subscribe to a channel
PubnubSubsystem->SubscribeToChannel(FString Channel, FOnSubscribeOperationResponse OnSubscribeToChannelResponse, FPubnubSubscribeSettings SubscribeSettings = FPubnubSubscribeSettings());
// subscribe to a channel group
PubnubSubsystem->SubscribeToGroup(FString ChannelGroup, FOnSubscribeOperationResponse OnSubscribeToGroupResponse, FPubnubSubscribeSettings SubscribeSettings = FPubnubSubscribeSettings());
* required
ParameterDescription
Channel *
Type: FString
The channel ID to subscribe to.
ChannelGroup *
Type: FString
The channel group to subscribe to.
OnSubscribeToChannelResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSubscribeOperationResponseNative to handle the result using a lambda.
OnSubscribeToGroupResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSubscribeOperationResponseNative to handle the result using a lambda.
SubscriptionSettingsStruct defining subscription configuration.

FOnSubscribeOperationResponse

FieldTypeDescription
Result
FPubnubOperationResult
The result of the operation.

FOnSubscribeOperationResponseNative

FieldTypeDescription
Result
const FPubnubOperationResult&
The result of the operation.

FPubnubSubscriptionSettings

* required
ParameterDescription
ReceivePresenceEvents
Type: bool
Whether to subscribe to presence events.

Sample code

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Actor.h



Actor.cpp



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

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Subscribe with presence

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

For any given channel there is an associated Presence channel. You can subscribe directly to the channel by appending -pnpres to the channel name or you can use the subscribe method with the ReceivePresenceEvents parameter set to true. For more information on presence events, refer to the Presence section.

Actor.h


Actor.cpp



Response

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

Subscribe to channel group

Actor.h


Actor.cpp



Subscribe with lambda

Actor.h


Actor.cpp



Subscribe with result callback

Actor.h


Actor.cpp



Unsubscribe

When subscribed to a single channel, this function causes the client to issue a leave from the channel ID 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)

// unsubscribe from a channel
PubnubSubsystem->UnsubscribeFromChannel(FString Channel, FOnSubscribeOperationResponse OnUnsubscribeFromChannelResponse);
// unsubscribe from a channel group
PubnubSubsystem->UnsubscribeFromGroup(FString ChannelGroup, FOnSubscribeOperationResponse OnUnsubscribeFromGroupResponse);
* required
ParameterDescription
Channel *
Type: FString
The channel ID to unsubscribe from.
ChannelGroup *
Type: FString
The channel group to unsubscribe from.
OnUnsubscribeFromChannelResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSubscribeOperationResponseNative to handle the result using a lambda.
OnUnsubscribeFromGroupResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSubscribeOperationResponseNative to handle the result using a lambda.

Sample code

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Actor.h



Actor.cpp



Returns

This method doesn't have a return value.

Other examples

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Unsubscribe from a channel group

Actor.h


Actor.cpp



Unsubscribe all

Unsubscribe from all channels and all channel groups.

Method(s)

PubnubSubsystem->UnsubscribeFromAll(FOnSubscribeOperationResponse OnUnsubscribeFromAllResponse);
* required
ParameterDescription
OnUnsubscribeFromAllResponseThe delegate for the operation's result.

You can also use a native callback of the type FOnSubscribeOperationResponseNative to handle the result using a lambda.

Sample code

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

Actor.h



Actor.cpp



Returns

This method doesn't have a return value.

Complete example

Reference code
Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code.

ASample_PubSubFull.h



ASample_PubSubFull.cpp



Last updated on