API Protocol Playground
The same small app, served five different ways: REST, GraphQL, gRPC, WebSockets, and Server-Sent Events.
The app is a kitchen order rail. Dishes have prices, orders have items, and orders move through received to cooking to ready to delivered. A simulator nudges orders forward every few seconds so there is always something live to watch.
Every server imports the exact same store from src/core/store.ts. Same data, same rules, same event bus. So any difference you notice between the five is a property of the protocol, not of the app. That is the entire point.
Quickstart
npm install
npm run dev # all five servers at once, color coded per protocol
| Protocol |
Port |
Entry point |
Try it |
| REST |
4001 |
src/rest/server.ts |
curl http://127.0.0.1:4001/orders/1 |
| GraphQL |
4002 |
src/graphql/server.ts |
open http://127.0.0.1:4002/graphql |
| gRPC |
4003 |
src/grpc/server.ts |
npm run client:grpc |
| WebSocket |
4004 |
src/websocket/server.ts |
open http://127.0.0.1:4004/ or npm run client:ws |
| SSE |
4005 |
src/sse/server.ts |
curl -N http://127.0.0.1:4005/events |
Ports come from src/core/ports.ts and can be overridden with REST_PORT, GRAPHQL_PORT, GRPC_PORT, WS_PORT, SSE_PORT.
Other scripts:
npm run smoke # exercises all five protocols end to end, exits nonzero on failure
npm run compare # same scenario over REST, GraphQL, and gRPC, with a table
npm run typecheck # tsc --noEmit
smoke and compare start any server that is not already listening and shut down only what they started, so they work whether or not npm run dev is running.
One thing to know: each server runs in its own Node process, so each has its own copy of the store. An order you place over REST will not appear on the GraphQL server. That keeps the comparison honest without dragging in a database.
The five, in plain language
REST
An HTTP request per thing you want. GET /orders/1 returns the order. The order lists dish ids, not dish objects, so if you want dish names you make more requests. Nouns are URLs, verbs are HTTP methods, outcomes are status codes.
- Transport: HTTP/1.1 (or HTTP/2, transparently)
- Wire format: JSON text
- Connection lifecycle: one request, one response, connection may be reused for the next one
- Who initiates: client only
- Shape: request/response
The strength is that everything already understands it. Browsers, curl, proxies, CDNs, load balancers, corporate firewalls, every HTTP library ever written. The weakness is that the server decides the response shape, so clients either make several calls (under-fetching) or download fields they will not use (over-fetching).
Full walkthrough with curl examples →
GraphQL
One endpoint, POST /graphql, and the client sends a query describing the exact tree of data it wants. Ask for an order with its items with their dishes, and all of it comes back in one response. Ask for only dish names and you get only dish names.
- Transport: HTTP, almost always a single POST endpoint
- Wire format: JSON, with a typed schema enforcing what is valid
- Connection lifecycle: same as REST for queries and mutations; subscriptions hold a connection open (this server uses SSE for them)
- Who initiates: client, except during a subscription
- Shape: request/response, plus subscriptions
The strength is that the client controls the response shape and the schema is machine-readable, which makes tooling excellent. The weaknesses are real too: HTTP caching mostly stops working when every read is a POST to the same URL, an expensive query can be arbitrarily deep, and resolvers invite N+1 database problems.
Full walkthrough with example queries →
gRPC
You write a .proto file describing your messages and methods. Both sides use it to generate code. Calls look like ordinary function calls, and the bytes on the wire are compact binary protobuf over HTTP/2.
- Transport: HTTP/2, multiplexed
- Wire format: binary protobuf, defined by a schema
- Connection lifecycle: one long-lived connection, many concurrent calls over it
- Who initiates: client opens the call, but streaming means either side can then send
- Shape: unary, server streaming, client streaming, bidirectional streaming
The strength is efficiency and rigor: the payload in this repo's benchmark is 139 bytes where REST needs 466, the schema is enforced by generated code, and streaming is a first class idea rather than an add-on. The weakness is reach. A browser cannot speak gRPC directly without a proxy (grpc-web), you cannot curl it, and debugging requires tools that can read your .proto.
Full walkthrough with grpcurl examples →
WebSockets
The connection starts as a normal HTTP GET carrying Upgrade: websocket. The server answers 101 Switching Protocols and from that moment the socket is no longer speaking HTTP. It is a full duplex pipe of frames, and either side may send at any time without being asked.
- Transport: TCP, after an HTTP upgrade handshake (
ws:// or wss://)
- Wire format: your choice, framed as text or binary. This repo uses JSON.
- Connection lifecycle: one long-lived connection, opened once and held
- Who initiates: both sides, symmetrically
- Shape: bidirectional streaming
The strength is that symmetry: chat, collaborative editing, multiplayer, live dashboards with client controls. The weakness is that the protocol gives you almost nothing above the frame. No status codes, no methods, no schema, no caching, no automatic reconnection. Every one of those is something you design and build yourself.
Full walkthrough with a browser demo →
Server-Sent Events
A plain HTTP GET whose response body never ends. The content type is text/event-stream and the body is a tiny line format: event:, data:, id:, blank line to dispatch. Browsers parse it natively with EventSource, and reconnect on their own when the connection drops.
- Transport: HTTP/1.1 or HTTP/2, one long-lived response
- Wire format: UTF-8 text in a line-oriented format
- Connection lifecycle: one open response held for as long as you like
- Who initiates: server pushes, client can only open or close the stream
- Shape: server to client streaming
The strengths are how little it costs you: it is HTTP all the way down, so proxies, auth headers, and status codes all still work, and automatic reconnect with Last-Event-ID replay is built into the browser. The weaknesses are that it is one directional (anything the client wants to say needs a separate request), it is text only, and over HTTP/1.1 browsers cap you at about 6 connections per origin.
Full walkthrough with curl examples →
Comparison table
|
REST |
GraphQL |
gRPC |
WebSocket |
SSE |
| Transport |
HTTP/1.1 or 2 |
HTTP (one endpoint) |
HTTP/2 |
TCP after HTTP upgrade |
HTTP, unending response |
| Wire format |
JSON text |
JSON text |
binary protobuf |
anything, framed |
UTF-8 text lines |
| Direction |
client asks |
client asks |
both (streaming) |
both, symmetric |
server pushes |
| Browser support |
native |
native (plain fetch) |
needs grpc-web proxy |
native WebSocket |
native EventSource |
| Typing / schema |
optional (OpenAPI) |
required, introspectable |
required (.proto) |
none, roll your own |
none, roll your own |
| Caching |
excellent, HTTP caching works |
poor, POST defeats it |
none built in |
none |
none |
| Errors |
status codes |
errors array, HTTP 200 |
status codes |
invent your own |
HTTP status on open only |
| Round trips for our scenario |
2 to 4 |
1 |
1 |
n/a (pushed) |
n/a (pushed) |
| Best for |
public APIs, CRUD, anything cacheable |
client-driven UIs, aggregating many sources |
internal service to service, low latency, polyglot |
chat, collaboration, anything with two-way live input |
live feeds, notifications, progress bars, log tails |
The numbers
npm run compare fetches order 1 with its dish details over each protocol and measures it. HTTP byte counts are read off the TCP socket, so they include headers. Sample run:
approach trips payload B wire B median ms what it costs you
------------------------ ----- --------- ------ --------- ------------------------------------------------
REST, one GET per dish 4 466 1698 2.83 under-fetching: the order only carries dish ids
REST, order + whole menu 2 819 1434 1.45 over-fetching: 7 dishes downloaded, 3 needed
GraphQL, one query 1 552 874 1.93 exactly the fields asked for, no more
gRPC, one unary call 1 139 n/a 2.57 binary protobuf, dishes embedded in the response
transport delay ms notes
------------------------- -------- -----------------------------------------------------------
WebSocket (subscribed) 0.44 one open socket, server pushes the frame immediately
SSE (streaming GET) 2.73 push down the open response, writes go over a separate POST
Polling REST every 1000ms 500.39 2 requests, 458 wire bytes returned nothing new
Read those carefully, because the interesting part is not the winner.
Latency here is server work, not network. Everything is on loopback, so a round trip costs microseconds. On a real network with 50ms of latency, the four-round-trip REST row would take roughly 200ms and the one-round-trip rows roughly 50ms. Round trips are the number that travels; milliseconds measured on localhost are not.
REST is not slow, it is chatty. The per-dish version moves fewer payload bytes than the whole-menu version and still loses, because it pays for four sets of HTTP headers. Headers are why wire bytes are three to four times payload bytes for the REST rows.
gRPC's 139 bytes is the protobuf story in one number. No field names on the wire (just field numbers), integers varint-encoded, enums as small integers. JSON repeats "priceCents" on every line of every order forever.
Polling's 500ms is arithmetic, not a fluke. Poll every N milliseconds and you wait N/2 on average. Halving the delay means doubling the request count, which is why every "make it feel live" project eventually reaches for push.
Repo layout
src/
core/ the shared store, types, seed data, and port table
rest/ Express, JSON over HTTP/1.1 README.md
graphql/ graphql-yoga, schema + resolvers README.md
grpc/ @grpc/grpc-js, orders.proto, demo client README.md
websocket/ ws, browser page, Node client README.md
sse/ Express, EventSource page, Node client README.md
scripts/
harness.ts starts/stops servers for the two scripts below
smoke.ts end to end checks for all five protocols
compare.ts the measurements above
Choosing, in one paragraph each
Reach for REST by default for anything public or cacheable. It is the option other people already know how to consume, and HTTP caching is a genuinely large performance feature you get for free.
Reach for GraphQL when many different clients need many different shapes of the same data, or when one screen aggregates several backends. Pay attention to the costs you take on: query depth limits, N+1 batching, and losing HTTP caching.
Reach for gRPC for service to service traffic inside your own network, especially across languages and especially when the calls are frequent enough that payload size and connection reuse matter. Do not put it at the browser edge without a good reason.
Reach for WebSockets when the client needs to send as freely as the server does. Chat, cursors, collaborative editing, game state. If your client only ever listens, this is more machinery than you need.
Reach for SSE when updates flow one way and you want to stay inside HTTP. Notifications, progress bars, live dashboards, streaming LLM tokens. It is the least amount of new surface area of anything here, and the browser handles reconnection for you.
Read every sub-README, then run npm run compare and watch the terminal logs of npm run dev while you poke each server. The request logs make the round trip counts obvious in a way a table cannot.