Skip to Content
Metadata

Metadata

Mesh provides two types of metadata, both stored in Redis and available across all server instances:

  • Connection metadata — tied to individual WebSocket connections
  • Room metadata — tied to specific room names

Metadata is useful for attaching information like user IDs, roles, tokens, room settings, or custom fields relevant to your app.


Connection metadata

Attach structured data to a WebSocket connection—commonly used for:

  • User identification (e.g. userId, role)
  • Session tokens
  • Enriching presence events
await server.connectionManager.setMetadata(connection, { userId: "user123", role: "admin", });

You can read metadata later:

const meta = await server.connectionManager.getMetadata("conn123");
💡

Connection metadata is automatically removed when the connection closes.


Room metadata

Store structured info about a room—like:

  • Topics or settings
  • Ownership and access rules
  • Game or session state
await server.roomManager.setMetadata("lobby", { topic: "General Discussion", createdBy: "user123", });

Partial updates are supported:

await server.roomManager.updateMetadata("lobby", { topic: "Updated Topic", });

To retrieve metadata:

const roomMeta = await server.roomManager.getMetadata("lobby");

Room metadata is removed automatically when you call clearRoom(...), or you can explicitly clear it by setting the value to null.


Accessing metadata from clients

Clients can retrieve metadata directly using built-in client methods:

Connection metadata

const meta = await client.getConnectionMetadata("conn123"); console.log(meta.userId, meta.username);

Get your own connection metadata by omitting the connection ID:

const meta = await client.getConnectionMetadata(); console.log(meta.userId, meta.username);

Room metadata

const roomMeta = await client.getRoomMetadata("lobby"); console.log(roomMeta.topic);

Best practices

  • Keep metadata small — avoid large or deeply nested objects
  • Don’t store sensitive data — expose only what’s safe to read
  • Use updateMetadata(...) for partial updates
  • Use consistent ID formats — e.g. "lobby", "user:123"

See Server SDK → Metadata and Client SDK → Presence for advanced usage.

Last updated on
© 2025