Cross-Server Messaging enables you to communicate to other servers or client instances of your experience. Normally, the code within an experience can only affect the server or clients that it's running on, but there might be situations where you want different servers to communicate with each other, including:
- Cross-Server Chat: Enable users to chat with each other even if they’re not in the same server.
- Global Announcements: Send announcements such as "A user found a special item!" in the experience to all servers.
- Realtime Server Browser: Show a constantly-updating list of servers and who is in them.
You can support cross-server messaging in your experience using MessagingService. You can also use the Teleportation Playground sample experience to see how cross-server messaging works before you implement it. To enable cross-server communication using external tools to improve your productivity, see Communicating to Live Servers from External Tools.
Set Up Cross-Server Messaging
To enable cross-server messaging, you must set up a topic, which is a customized message channel that is accessible from multiple servers. After you create a topic, you can subscribe users to the topic to receive messages and enable publishing messages to the topic.
Subscribe Users to Receive Messages
Use MessagingService:SubscribeAsync() to subscribe users to a topic and specify a callback function that detects messages publishing to that topic. For example, the following code sample subscribes all users to a FriendServerEvent topic that receives messages when any user is teleported to a different server.
1local MessagingService = game:GetService("MessagingService")2local Players = game:GetService("Players")34local MESSAGING_TOPIC = "FriendServerEvent"56Players.PlayerAdded:Connect(function(player)78 -- Subscribe to the topic9 local subscribeSuccess, subscribeConnection = pcall(function()10 return MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)11 print(message.Data)12 end)13 end)14 if subscribeSuccess then15 -- Unsubscribe from topic upon player ancestry change16 player.AncestryChanged:Connect(function()17 subscribeConnection:Disconnect()18 end)19 end20end)21
Use MessagingService:PublishAsync() to match a specific topic and publish a message to it. For example, the following code sample uses MessagingService:PublishAsync() to notify all users when a user joins a new server, including the Player.Name representing the user's display name and the JobId, a unique identifier for the running experience server instance.
1local MessagingService = game:GetService("MessagingService")2local Players = game:GetService("Players")34local MESSAGING_TOPIC = "FriendServerEvent"56Players.PlayerAdded:Connect(function(player)78 -- Publish to topic9 local publishSuccess, publishResult = pcall(function()10 local message = player.Name .. " joined server with 'JobId' of " .. game.JobId11 MessagingService:PublishAsync(MESSAGING_TOPIC, message)12 end)13 if not publishSuccess then14 print(publishResult)15 end16end)17