Skip to content

Menu

Webhooks and WebSockets Cheat Sheet: Signatures, Retries, Close Codes

Checked against Webhooks (Stripe, GitHub) and WebSocket (RFC 6455) Stripe and GitHub webhook docs as of 28 Nov 2026; RFC 6455; Node 24.4.0 for the examples on 28 November 2026. Each section links to the official docs it was checked against.

This sheet covers the two ways servers push events: webhooks, where the provider calls your HTTP endpoint, and WebSockets, where both sides keep a socket open. The webhook rows follow Stripe's and GitHub's own docs as read on 28 November 2026, and the WebSocket rows follow RFC 6455. The signature and handshake examples were run in Node 24.4.0.

Receiving a webhook, opening a WebSocketA webhook handler verifies the raw body, skips repeats and acknowledges fast; a WebSocket starts as an HTTP GET that the server switches with 101.WebhookProvider POSTs an eventHMAC-SHA256 over the raw bodycompare with crypto.timingSafeEqualmismatch → rejectStripe: check timestampdefault tolerance 300 secondsEvent ID already processed?yes → skip itReturn 2xxthen process from a queueWebSocketClientServerGET, Upgrade: websocketSec-WebSocket-Key, Version: 13101 Switching ProtocolsSec-WebSocket-AcceptFrames both waystext, binary, ping/pong, close
A webhook handler verifies the raw body, skips repeats and acknowledges fast; a WebSocket starts as an HTTP GET that the server switches with 101.

When to use what

Use a webhook when the event comes from someone else's system and your side can be a plain HTTPS endpoint. Use a WebSocket when your own client needs low-latency, two-way messages, such as chat or live cursors. If the browser only needs to listen, the WebSocket vs SSE guide explains why Server-Sent Events can be simpler.

For webhooks, assume three things: anyone can POST to your URL, the same event will arrive twice, and events will arrive out of order. Stripe says all three in its docs. So verify the signature on the raw bytes before parsing, record the event ID before acting, and fetch the current object from the API when order matters. Return a 2xx quickly and do the slow work from a queue.

Our webhook tester shows exactly what a provider sends, the payload builder makes test bodies, and the HMAC generator checks a signature by hand. For sockets, the WebSocket tester opens a connection from the browser. The webhooks implementation guide covers queueing in more depth.

GitHub's header and test vector are documented at https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries.

X-Hub-Signature-256: sha256=<hex>

GitHub: HMAC-SHA256 of the raw body with your webhook secret, hex-encoded with a sha256= prefix

Stripe-Signature: t=<unix>,v1=<hex>

Stripe: HMAC-SHA256 over t + "." + raw body; check every v1 entry and ignore v0

crypto.timingSafeEqual(a, b)

Compare signatures in constant time; GitHub's docs say never to use a plain ==

verify the raw request body

Stripe requires the unmodified body; re-serialised JSON changes the bytes and the signature fails

tolerance: 300 seconds

Stripe's libraries reject timestamps more than 5 minutes off by default; never set tolerance to 0

return 2xx before slow work

Acknowledge quickly and process from a queue; slow handlers time out and get retried

From the docs, not run by us
Stripe live mode: retries for up to three days

Failed deliveries are retried with exponential back-off; sandbox events are retried three times over a few hours

From the docs, not run by us
3xx counts as failure

Stripe treats a redirect as a failed delivery, so register the final URL

From the docs, not run by us
log processed event IDs

Skip events you already handled; Stripe says the same event can arrive more than once

From the docs, not run by us
don't sort by `created`

Stripe doesn't guarantee order and created has one-second resolution; fetch the latest object instead

From the docs, not run by us
stripe listen --forward-to localhost:4242/webhook

Forward Stripe events to a local server while developing (Stripe CLI)

Try it: Webhook TesterFrom the docs, not run by us
GET /chat + Upgrade: websocket + Connection: Upgrade

Start the handshake as a normal HTTP/1.1 GET asking to switch protocols

Try it: websocket-testerFrom the docs, not run by us
Sec-WebSocket-Version: 13

The only version RFC 6455 defines; servers reject others

From the docs, not run by us
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

A random 16-byte value, base64-encoded, sent by the client

From the docs, not run by us
101 Switching Protocols + Sec-WebSocket-Accept

Server proof: base64(SHA-1(key + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11))

Sec-WebSocket-Protocol: chat

Pick one subprotocol from the client's list, or omit the header to pick none

From the docs, not run by us

Clients must mask every frame they send; servers must not mask theirs. Control frames carry at most 125 bytes and can't be fragmented.

0x1 text / 0x2 binary / 0x0 continuation

Data frame opcodes; text must be valid UTF-8

From the docs, not run by us
0x9 ping -> 0xA pong

Reply to a ping with a pong carrying the same payload; an unsolicited pong works as a one-way heartbeat

From the docs, not run by us
0x8 close

Start or answer the closing handshake, optionally with a 2-byte status code and a reason

From the docs, not run by us
1000 / 1001

Normal closure / endpoint going away, such as a server restart or a page navigation

From the docs, not run by us
1002 / 1003 / 1007

Protocol error / data type not accepted / payload inconsistent with its type, such as bad UTF-8

From the docs, not run by us
1008 / 1009 / 1011

Policy violation / message too big / server hit an unexpected condition

From the docs, not run by us
1005, 1006, 1015

Reserved for reporting locally (no code, abnormal close, TLS failure); never send them in a close frame

From the docs, not run by us

Worked example: Verify a GitHub and a Stripe-style signature in Node

GitHub publishes a test vector, so you can prove your code works before wiring it to a real endpoint:

import crypto from 'node:crypto';
const secret = "It's a Secret to Everybody";
const header = 'sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17';
function verify(rawBody) {
  const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
  const a = Buffer.from(expected), b = Buffer.from(header);
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}
console.log(verify('Hello, World!'), verify('Hello, World?'));

That printed true false. The length check comes first because timingSafeEqual throws on buffers of different sizes. We then signed 1798765432. plus a small JSON body the way Stripe's manual verification steps describe, put it in a header with a fake v0 entry, and ran three checks with a 300-second tolerance. Sixty seconds after the timestamp it returned ok. Six hundred seconds after, the signature still matched but the check returned too old, which is the replay protection. Finally we parsed the body and pretty-printed it with JSON.stringify(obj, null, 2) before verifying, and got bad signature. That last case is the bug people hit in Express: a JSON body parser runs first, and the bytes you hash are no longer the bytes Stripe signed. For the handshake, base64(sha1(key + GUID)) on RFC 6455's sample key gave s3pPLMBiTxaQ9kYGzzhZRbK+xOo=, matching the RFC.

Common mistakes

  • Verifying the signature against JSON.stringify(req.body) after a JSON middleware parsed it.

    Capture the raw request bytes for the webhook route and verify those; parse only after the check passes.

  • Comparing the two signature strings with ===.

    Use crypto.timingSafeEqual (Node) or your language's constant-time compare, after checking the lengths match.

  • Doing the full order fulfilment inside the webhook request.

    Store the event, return 2xx, and process from a queue. Slow handlers time out and trigger retries, which then double the work.

  • Assuming each event arrives once and in order.

    Keep a table of processed event IDs and, when order matters, fetch the current state of the object from the provider's API.

  • Sending close code 1006 from a server when a client misbehaves.

    1006 is reserved for reporting an abnormal close locally. Send 1002 for protocol errors or 1008 for policy violations.