PcoWSkbVqDnWTu_dm2ix

Cross-Server Messaging

Cross-Server Messaging

Normally, code within a game can only affect the server/clients that the code is running on, but sometimes you’ll need different servers to communicate with each other. This can be accomplished with cross-server messaging offered through MessagingService.

Some examples of MessagingService usage include:

  • Cross-Server Chat — Players can chat with each other even if they’re not in the same server, assuming a maximum of about 20 players.
  • Realtime Server Browser — Show a constantly-updating list of servers and who is in them.
  • Global Announcements — Let all servers know that a player found a special item in the game.

MessagingService Functions

MessagingService offers two core functions:

SubscribeAsync()

MessagingService/SubscribeAsync|SubscribeAsync() subscribes to a developer-defined “topic” and specifies a callback function that detects when messages are published to that topic. For example, a game might subscribe all players to a "FriendServerEvent" topic which can then receive messages from MessagingService/PublishAsync|PublishAsync() when any player teleports to a different server.

Usage Notes
  • The callback function for MessagingService/SubscribeAsync|SubscribeAsync() receives a table, not a string. This table contains both message.Data (the developer-defined data sent via MessagingService/PublishAsync|PublishAsync()) and message.Sent which is the Unix time in seconds at which the message was sent.
  • As seen on lines 16-18, it's recommended that you call Disconnect() on the datatype/RBXScriptConnection|connection when it's no longer necessary to listen for incoming messages to the topic.

PublishAsync()

MessagingService/PublishAsync|PublishAsync() publishes a message to a specific topic, upon which the callback function for any matching topic will be triggered. For instance, when a player joins a new server, MessagingService/PublishAsync|PublishAsync() could notify all other players about who just joined that server, along with its DataModel/JobId|JobId.

MessagingService Playground

Now that you understand basic cross-server messaging, see how it works within a sample game.

Teleportation Playground

Monitor when players teleport between places in a game and which specific server they teleport to.
Tags:
  • server
  • cross-server
  • communication