Send and receive messages
Asynchronous and synchronous method execution
Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.
-
Asynchronous methods (
Asyncsuffix) returnvoidand take an optional delegate parameter that fires when the operation completes.1Channel->SendTextAsync("Hello", OnSendTextResponseDelegate, SendTextParams);You can also use native callbacks that accept lambdas instead of dynamic delegates. Native callback types have the
Nativesuffix (for example,FOnPubnubChatOperationResponseNative). -
Synchronous methods (no suffix) block the main game thread until the operation completes and return a result struct directly.
1FPubnubChatOperationResult Result = Channel->SendText("Hello", SendTextParams);
Requires Message Persistence
Enable Message Persistence on your keyset in the Admin Portal to store messages.
Send
Use SendText() to send a message to a channel. The method handles text content, metadata, user mentions, and channel references.
Method signature
- C++ / Input parameters
- Blueprint
1Channel->SendText(
2 FString Message,
3 FPubnubChatSendTextParams SendTextParams = FPubnubChatSendTextParams()
4);
Input parameters
| Parameter | Description |
|---|---|
Message *Type: FStringDefault: n/a | Text that you want to send to the selected channel. |
SendTextParamsType: FPubnubChatSendTextParamsDefault: n/a | Struct providing additional parameters. |
→ StoreInHistoryType: boolDefault: true | If true, the messages are stored in Message Persistence. If StoreInHistory is not specified, the Message Persistence configuration specified on the Admin Portal keyset is used. |
→ SendByPostType: boolDefault: false | When true, the SDK uses HTTP POST to publish the messages. The message is sent in the BODY of the request instead of the query string when HTTP GET is used. The messages are also compressed to reduce their size. |
→ MetaType: FStringDefault: n/a | Publish additional details with the request. |
Sending quoted messages
SendText() does not expose quoting directly. To send a quoted message, use a MessageDraft: call MessageDraft->SetQuotedMessage(QuotedMessage) before Draft->Send().
Output
| Type | Description |
|---|---|
FPubnubChatOperationResult | Result of the operation. Check Error and ErrorMessage for failure. |
Sample code
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.
Send a text message to a channel asynchronously.
Receive
To receive messages on a given channel, you must connect to the channel and start listening to message events.