The WebSocket stream

Events pushed over a WebSocket the moment they commit: connecting, the frames in both directions, replay with last_event_id, the limits and the close codes.

On this page
  1. Connecting
  2. Frames
  3. Replay after a disconnect
  4. Limits and close codes

Connecting

GET /v1/ws upgrades to a WebSocket. Authenticate with the Authorization: Bearer dm_... header, or with ?token=dm_... on the URL for a client that cannot set headers, such as a browser. Console sessions are not accepted on the stream. An app token for the REST resource needs events:read and mail:read; the default agent permissions do not include the first. An account key sees every inbox; an inbox key sees its own and cannot subscribe to others.

Idle connections cost nothing on the server and sleep at the edge; the inbound pipeline pushes each event to the subscribers right after it is stored, before the webhook queue, so the stream is the earliest an agent can learn about mail.

import WebSocket from "ws";

const socket = new WebSocket("wss://api.doorman.fyi/v1/ws", { headers: { authorization: `Bearer ${process.env.DOORMAN_API_KEY}` } });
// A client that cannot set headers (a browser) passes the key as ?token=dm_... instead.
socket.addEventListener("open", () => socket.send(JSON.stringify({"type":"subscribe","inbox_ids":["inb_01jc4q7w9k2m5n8p3r6t1v4x7z"]})));
socket.addEventListener("message", (message) => {
  const frame = JSON.parse(String(message.data));
  if (frame.type === "subscribed") console.log("subscribed", frame.inbox_ids);
  if (frame.event === "message.received") console.log(frame.event_id, frame.message.subject);
});

Frames

Both sides send JSON text frames. The client's frames are the StreamClientFrame schema:

subscribe
Start receiving events: everything the key can see, or a narrower set with inbox_ids (up to 32, all of them the key's) and event_types. Subscriptions accumulate. Add last_event_id to replay what was missed, below.
unsubscribe
Stop receiving: everything, or the inboxes named in inbox_ids.
ping
A keepalive the edge answers with pong without waking the stream. Send one every 30 to 60 seconds through idle proxies.

The server's frames are the StreamServerFrame schema:

subscribed
The subscription in force after a subscribe frame, with the inbox ids and event types; empty arrays mean all.
unsubscribed
The answer to an unsubscribe frame, echoing the inboxes it named.
pong
The answer to a ping.
error
A frame that could not be honoured, with a message. The connection stays open.
An event
The webhook payload plus delivered_at and attempt, the StreamEvent schema. attempt is always 1 on the stream.
{
  "type": "subscribe",
  "inbox_ids": [
    "inb_01jc4q7w9k2m5n8p3r6t1v4x7z"
  ]
}

Replay after a disconnect

A push that fails is not retried over the socket. Keep the event_id of the last event you handled and, on reconnect, subscribe with it as last_event_id: the server replays every event after it, up to 100 and filtered by the subscription, before the live ones. An id the account does not know answers an error frame; a client that was away for longer than 100 events catches up through the event log instead.

Subscribe and replay

{
  "type": "subscribe",
  "inbox_ids": [
    "inb_01jc4q7w9k2m5n8p3r6t1v4x7z"
  ],
  "last_event_id": "evt_01jc5e5f6g7h8j9k0m1n2p3q4r"
}

Limits and close codes

  • 10 open connections per account; the next one is closed with 4008.
  • 30 frames per 10 seconds per connection; more closes it with 4029.
  • A client frame may be 16 KiB at most, and a subscribe frame may name 32 inboxes.
  • A revoked key, a deleted inbox or a suspended account closes its connections with 4001.
Close codeMeaningWhat to do
4001The key was revoked, the inbox deleted or the account suspended.Do not reconnect with the same key.
4008The account already has 10 connections open.Close one, or share a connection between workers.
4029The connection sent frames too fast.Reconnect with a slower sender; one subscribe and a ping a minute is plenty.

Any other close, including a network drop, is ordinary: reconnect with a short backoff and last_event_id.

Up Down to move, Enter to open, Esc to close