MessagingService
MessagingService : Instance
Overview
MessagingService is a service that lets multiple servers running in the same world send and receive messages by topic. Use it when you need to send real-time signals to other servers, such as cross-server announcements or notifications of state changes on other servers.
When you send a message to a specific topic with PublishAsync(), the callbacks of the servers that subscribed to that topic with SubscribeAsync() are called.
This service can only be used in server Scripts, and calling it from a LocalScript raises an error.
Important Notes
Message delivery is not guaranteed (best-effort). For data that must be applied, use DataStoreService or MemoryStoreService together with it.
A topic name cannot be empty and cannot exceed 80 bytes in UTF-8.
When testing in Studio, published messages are delivered only to subscribers in the same session.
Properties
Methods
PublishAsync
Publishes a message to the specified topic.
This method yields the calling script until the platform accepts the publish request. If there is no response within 30 seconds or the publish fails, an error is raised, so wrapping calls in pcall is recommended.
Parameters
string InTopic
The name of the topic to publish the message to.
An empty string or a name exceeding 80 bytes in UTF-8 raises an error.
Value InMessage
(Optional) The message data to publish.
You can pass nil, boolean, number, string, buffer, and tables composed of these.
A serialized size exceeding 1KB, nesting that is too deep, or too many elements raises an error.
Passing a string that is not valid UTF-8, a table with circular references, or a value of an unsupported type raises an error.
Return
void
Code Samples
SubscribeAsync
Subscribes to the specified topic and registers a callback to be called whenever a message arrives on that topic.
It immediately returns the connection object of the registered callback. To end the subscription, call Disconnect() on the returned connection object.
The number of subscriptions a single server can maintain is limited to 20 + 8 × (current number of players), and exceeding it raises an error.
Parameters
string InTopic
The name of the topic to subscribe to.
An empty string or a name exceeding 80 bytes in UTF-8 raises an error.
Value InCallback
The function to call when a message arrives.
The callback receives a single table with the following fields as its argument.
Data : The published message data. This field is absent if the message was published without data.
Sent : The time the message was published (Unix time, in seconds).
Passing a value that is not a function raises an error.
Return
ScriptConnection
The connection object of the registered callback. Calling Disconnect() unsubscribes.
Code Samples
Events
Last updated