mcpField
S
serverpod-server-events

serverpod-server-events

github.com/serverpodDevelopment3.3k43 installsUpdated 1d ago
Repository

About serverpod-server-events

Serverpod Server Events provides named-channel messaging for Serverpod backends. It is intended for developers building server-side event flows, with local delivery or cluster-wide delivery when Redis is enabled.

Key features

  1. 01Post serializable messages to named channels
  2. 02Choose local or global delivery scope
  3. 03Receive messages through streams
  4. 04Register and remove channel listeners
  5. 05Clean up listeners when sessions close

Use cases

  1. 01Broadcast user updates on the server
  2. 02Keep events within one server instance
  3. 03Send events across a cluster with Redis
  4. 04Handle event messages through typed streams

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/serverpod/serverpod --skill serverpod-server-events

After installing, type/serverpod-server-eventsin Claude Code to confirm it loaded.

Details

From the project README

Serverpod Server Events

Event messaging via session.messages on named channels. Messages must be serializable models. By default (MessageScope.auto) messages are delivered across the cluster when Redis is enabled, otherwise locally within the server instance.

Sending

await session.messages.postMessage('user_updates', UserUpdate(...));

// Restrict delivery with a scope:
await session.messages.postMessage('user_updates', message, scope: MessageScope.local);
await session.messages.postMessage('user_updates', message, scope: MessageScope.global);

MessageScope.local delivers synchronously within this server only. MessageScope.global requires Redis and throws a StateError if it is not enabled. With Redis enabled, delivery is asynchronous and best effort, and listeners receive a deserialized copy of the message rather than the posted instance.

Receiving

Stream:

var stream = session.messages.createStream<UserUpdate>('user_updates');
stream.listen((message) => print('Received: $message'));

If a message on the channel is not of type T, the stream emits an error. Use exact serializable types or a deliberate shared base type.

Listener:

session.messages.addListener<UserUpdate>('user_updates', (message) {
  print('Received: $message');
});

Both receive local and global messages. Streams/listeners are removed when the session closes. Remove manually with session.messages.removeListener(channel, callback). Models support inheritance, which is useful when wanting a fully typed interface for server events.

Comments

Markdown supported