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.
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 bodyStripe requires the unmodified body; re-serialised JSON changes the bytes and the signature fails
tolerance: 300 secondsStripe's libraries reject timestamps more than 5 minutes off by default; never set tolerance to 0
return 2xx before slow workAcknowledge quickly and process from a queue; slow handlers time out and get retried
Stripe live mode: retries for up to three daysFailed deliveries are retried with exponential back-off; sandbox events are retried three times over a few hours
3xx counts as failureStripe treats a redirect as a failed delivery, so register the final URL
log processed event IDsSkip events you already handled; Stripe says the same event can arrive more than once
don't sort by `created`Stripe doesn't guarantee order and created has one-second resolution; fetch the latest object instead
stripe listen --forward-to localhost:4242/webhookForward Stripe events to a local server while developing (Stripe CLI)
GET /chat + Upgrade: websocket + Connection: UpgradeStart the handshake as a normal HTTP/1.1 GET asking to switch protocols
Sec-WebSocket-Version: 13The only version RFC 6455 defines; servers reject others
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==A random 16-byte value, base64-encoded, sent by the client
101 Switching Protocols + Sec-WebSocket-AcceptServer proof: base64(SHA-1(key + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11))
Sec-WebSocket-Protocol: chatPick one subprotocol from the client's list, or omit the header to pick none
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 continuationData frame opcodes; text must be valid UTF-8
0x9 ping -> 0xA pongReply to a ping with a pong carrying the same payload; an unsolicited pong works as a one-way heartbeat
0x8 closeStart or answer the closing handshake, optionally with a 2-byte status code and a reason
1000 / 1001Normal closure / endpoint going away, such as a server restart or a page navigation
1002 / 1003 / 1007Protocol error / data type not accepted / payload inconsistent with its type, such as bad UTF-8
1008 / 1009 / 1011Policy violation / message too big / server hit an unexpected condition
1005, 1006, 1015Reserved for reporting locally (no code, abnormal close, TLS failure); never send them in a close frame
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.
Related cheat sheets
Sources
- https://docs.stripe.com/webhooks
- https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries
- https://www.rfc-editor.org/rfc/rfc6455.html
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.