Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Realtime WebSocket API (BEAM Edition)

Building a Realtime WebSocket API (BEAM Edition)

Sometimes consumers of your APIs require near-realtime communication because regular RESTful HTTP apis can be a few milliseconds too slow. These performant and scalable APIs can be made over websocket TCP connections where events are pushed from client and server in near-realtime fashion.

This talk is a story of how I built such an API. We'll look at why this decision to create a websocket API was made and we will take a look at the data that supported this decision. We will take a deep dive into Phoenix websockets, channels, and transports to expose the underlying architecture. Finally, we look at how we tested the API, how we authenticated users over the channels, and how Phoenix helped this all happen with relative ease.

Jamie Wright

March 05, 2020
Tweet

More Decks by Jamie Wright

Other Decks in Programming

Transcript

  1. Server is gold copy Server pushes events to clients Clients

    displays event Source One #CodeBEAMSF
  2. Connecting Give clients a starting point Identify events from a

    single endpoint (client) Opportunity to authenticate #CodeBEAMSF
  3. Problems { topic: “events:1234”, event: “phx_join” } { topic: “events:1234”,

    event: “phx_reply”, payload: { ok: true, token: “1234” }}} WS WS WS WS Joining Channels #CodeBEAMSF
  4. Problems Joining Channels Remove complicated setup from clients Build reconnection/

    heartbeat logic :cowboy_websocket https://bit.ly/2WjtKxZ #CodeBEAMSF
  5. Review POST /graphql { url: “ws://…”, token: “1234” } HTTP

    HTTP HTTP HTTP Connecting { mutation: { connect: { … } } #CodeBEAMSF
  6. Review [“timers:start”, { duration: 1500, timestamp: 1273485 } ] [“timers:started”,

    { id: 1234, duration: 1500, remaining_time: 1500, status: “running” } ] WS WS WS WS Sending Messages #CodeBEAMSF