Server Messaging

Game servers can communicate with each other using the built-in Server Messaging service. This provides publish/subscribe functionality for sending messages between different game servers in real-time.

Overview

// Server Messaging can only be used on game servers. Calls made on the client will fail.
if (!Game.IsServer()) return;

// Subscribe to a topic to receive messages
const subscription = Platform.Server.Messaging.Subscribe("player-events", (data) => {
    print("Received message:", data);
});

// Publish a message to all subscribers of a topic
Platform.Server.Messaging.Publish("player-events", {
    event: "player-joined",
    userId: "12345",
    serverName: "Battle Arena 1"
}).then((result) => {
    if (result.success) {
        print("Message published successfully");
    }
});

// Unsubscribe when no longer needed
subscription.unsubscribe();

// Alternatively, if you are using a Bin to manage resources,
// the subscription can be added directly to it.
// this.bin.Add(subscription);

Use Cases

Server Messaging is useful for coordinating game state and events across multiple servers:

  • Cross-Server Events - Notify all servers when a global event occurs (e.g., world boss spawned)

  • Player Tracking - Share player status updates across servers

  • Global Announcements - Send announcements that should appear on all servers

API Reference

Subscribe

Subscribes to a topic, allowing you to receive messages published to that topic.

Parameters:

  • topic - The topic name to subscribe to (alphanumeric characters, underscores, and hyphens only)

  • callback - Function called when a message is received on the subscribed topic

Returns:

  • An object with an unsubscribe function to stop receiving messages. This object can also be passed to a Bin for automatic cleanup.

Publish

Publishes data to a topic, allowing other subscribers to receive the message.

Parameters:

  • topic - The topic name to publish to (alphanumeric characters, underscores, and hyphens only)

  • data - The data to be sent (will be JSON serialized)

Returns:

  • A Promise that resolves to an object with a success boolean, set to true if publishing was successful, false if not. You can retry failed messages later or drop them

circle-info

Server Messaging is designed for real-time communication between servers. Sometimes messages may not be delivered, for example if there are network issues . Always check the success flag when publishing messages and handle failures appropriately.

Topic Names

Topic names must follow these rules:

  • Only alphanumeric characters, underscores (_), and hyphens (-) are allowed

  • Topic names are case-sensitive

  • Examples: player-events, global_announcements, server123_status

Common Patterns

Global Event Broadcasting

Cross-Server Player Tracking

Periodic Status Updates

Best Practices

  • Unsubscribe when done - Always use a Bin or call unsubscribe() when you no longer need to receive messages to prevent memory leaks.

  • Use descriptive topic names - Choose clear, consistent naming conventions for your topics

  • Handle errors gracefully - Check the success flag when publishing and handle failures appropriately

  • Keep messages small - Avoid sending large data objects; consider using references to shared data stored in the Data Store or Cache Store

  • Avoid message loops - Be careful not to create infinite loops where servers keep responding to each other's messages

Limitations

  • Messages are not guaranteed to be delivered (fire-and-forget)

  • No message ordering guarantees between different topics

  • Topic names are limited to alphanumeric characters, underscores, and hyphens

  • Messages are JSON serialized, so complex objects may lose information

  • Server Messaging is only available on game servers, not on clients

Last updated