> For the complete documentation index, see [llms.txt](https://docs.overdare.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.overdare.com/development/api-reference/classes/messagingservice.md).

# 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  | <p>The name of the topic to publish the message to.</p><ul><li>An empty string or a name exceeding 80 bytes in UTF-8 raises an error.</li></ul>                                                                                                                                                                                                                                                  |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Value` InMessage | <p>(Optional) The message data to publish.</p><ul><li>You can pass nil, boolean, number, string, buffer, and tables composed of these.</li><li>A serialized size exceeding 1KB, nesting that is too deep, or too many elements raises an error.</li><li>Passing a string that is not valid UTF-8, a table with circular references, or a value of an unsupported type raises an error.</li></ul> |

#### 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   | <p>The name of the topic to subscribe to.</p><ul><li>An empty string or a name exceeding 80 bytes in UTF-8 raises an error.</li></ul>                                                                                                                                                                                                                                                                      |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Value` InCallback | <p>The function to call when a message arrives.</p><ul><li>The callback receives a single table with the following fields as its argument.</li><li>Data : The published message data. This field is absent if the message was published without data.</li><li>Sent : The time the message was published (Unix time, in seconds).</li><li>Passing a value that is not a function raises an error.</li></ul> |

#### Return

| `ScriptConnection` | The connection object of the registered callback. Calling Disconnect() unsubscribes. |
| ------------------ | ------------------------------------------------------------------------------------ |

#### Code Samples

## Events
