Skip to content
Meeko
Type to search the guides and the API reference. Open it from any page with / or Ctrl K; move with the arrow keys and open a result with Enter.
Get started

Contents

On this page

socketRoomName

The Durable Object name of a Socket room in Cloudflare Workers, with one object per channel and room, for idFromName.

adapter Since 1.0.0 @meekojs/meeko/cloudflare Source code
ts
import { socketRoomName } from "@meekojs/meeko/cloudflare";

socketRoomName(channel, room) returns the name Meeko uses to locate a room's Durable Object: the channel and room separated by a null character ("chat\u0000general"). It is used by createWorker when forwarding an upgrade, by DurableObjectSocketPubSub when publishing and by the room itself to recognize publications addressed to it.

Use it in a custom Worker that needs to communicate with a specific room: inspect it, publish without going through the hub or forward an upgrade.

Usage#

ts
import { socketRoomName } from "@meekojs/meeko/cloudflare";

const stub = env.MEEKO_SOCKET.get(env.MEEKO_SOCKET.idFromName(socketRoomName("chat", "general")));

Type#

ts
function socketRoomName(channel: string, room: string): string

Parameters#

Parameter Type Description
channel string The channel, the filename in server/socket/.
room string The room from ?room=; connections without a room use "".

Return value#

`${channel}\0${room}`. The null separator cannot appear in a channel name, so two distinct pairs never share an object.

Examples#

The name of each pair#

ts
import { socketRoomName } from "@meekojs/meeko/cloudflare";

console.log(JSON.stringify(socketRoomName("chat", "general"))); // "chat\u0000general"
console.log(JSON.stringify(socketRoomName("operations", "")));  // "operations\u0000", the default room

Publish to a room from a Cron Trigger#

ts
// worker.ts: a scheduled event rather than an HTTP request.
import { DurableObjectSocketPubSub } from "@meekojs/meeko/cloudflare";

export default {
  fetch: worker.fetch,
  async scheduled(_event: ScheduledEvent, env: Env) {
    const pubsub = new DurableObjectSocketPubSub(env.MEEKO_SOCKET);
    await pubsub.publish("status", "", { event: "maintenance", payload: { at: Date.now() } });
  },
};

DurableObjectSocketPubSub computes the name with socketRoomName. Publishing this way does not validate the event or payload before sending them (the room discards a payload that fails the schema when delivering it); within a request, prefer socket.status.broadcast("", "maintenance", payload), which validates them against the contract.

Common mistakes#

Build the name by hand#

Incorrectts
env.MEEKO_SOCKET.idFromName(`${channel}:${room}`);

There is no error, but this is a different object: empty, without the room's peers. The publications reach nobody.

Correctts
env.MEEKO_SOCKET.idFromName(socketRoomName(channel, room));

Notes#

  • Long names. If the name exceeds 1,024 bytes, the runtime does not expose ctx.id.name: the room takes it from its first connection or publication.
  • Stability. Deployed rooms are located by this name: a custom Worker that computes it differently communicates with empty objects.

Was this page helpful?

Add a comment

It is sent when you choose “Yes” or “No”. Only the page, your answer and the comment are stored: no cookies and no personal data.

0 of 2000 characters

Edit on GitHub: docs/en/reference/adapters/socketroomname.md