MessagingService

Show Deprecated
Not Creatable
Service
Not Replicated

The MessagingService allows game servers in the same game to communicate with each other in real time (< 1 second) using topics. Topics are developer defined strings (1-80 characters) that game servers can send and receive messages.

Delivery is best effort and not guaranteed. Make sure to architect your game so delivery failures are not critical.

Limitations

Note: these limits are subject to change.

LimitMaximum
Size of message 1kB
Messages sent per game server 150 + 60 * (number of players in this game server) per minute
Messages received per topic (10 + 20 * number of servers) per minute
Messages received for entire game (100 + 50 * number of servers) per minute

Cross-Server Messaging explores how to communicate between game servers in greater detail with relevant code samples.

Summary

Properties

Events

Methods

PublishAsync(topic: string, message: Variant): void  YIELDS

Invokes the supplied callback whenever a message is pushed to the topic.

SubscribeAsync(topic: string, callback: function): RBXScriptConnection  YIELDS

Begins listening to the given topic.

Properties

Events

Methods

PublishAsync

void
Yields

This function sends the provided message to all subscribers to the topic, triggering their registered callbacks to be invoked.

Yields until the message is received by the backend.

Parameters

topic: string

Determines where the message is sent.

message: Variant

The data to include in the message.


Returns

void

SubscribeAsync

Yields

This function registers a callback to begin listening to the given topic. The callback is invoked when a topic receives a message. It can be called multiple times for the same topic.

Callback

The callback is invoked with two arguments:

FieldSummary
Data Developer supplied payload
Sent Unix time in seconds at which the message was sent

It yields until the subscription is properly registered and returns a connection object.

To unsubscribe, call :Disconnect() on the returned object. Once Disconnect() is called, the callback should never be invoked. Killing the script containing the connections also causes the underlying connect to be unsubscribed.

See also:

Parameters

topic: string

Determines where to listen for messages.

callback: function

Function to be invoked whenever a message is received.


Returns

Connection that can be used to unsubscribe from the topic.

Code Samples

Subscribing to Cross Server Messages

1local MessagingService = game:GetService("MessagingService")
2local Players = game:GetService("Players")
3
4local function onPlayerAdded(player)
5 --subscribe to the topic
6 local topic = "player-" .. player.UserId
7 local connection = MessagingService:SubscribeAsync(topic, function(message)
8 print("Received message for", player.Name, message)
9 end)
10
11 player.AncestryChanged:Connect(function()
12 -- unsubscribe from the topic
13 connection:Disconnect()
14 end)
15end
16
17Players.PlayerAdded:Connect(onPlayerAdded)