Skip to main content

Module relay

Module relay 

Source
Expand description

Relaying broadcasts back into the sockets this process holds.

rainier-websocket says, in its own documentation, that a Rooms registry lives in one process’s memory and that two replicas behind a load balancer therefore have two sets of rooms. This is the module that stops that being true.

  POST /orders/7/ship  ─▶ replica A ─▶ Broadcasting ─▶ kafka topic
                                                           │
                                       ┌───────────────────┴────────────┐
                                       ▼                                ▼
                                relay on replica A              relay on replica B
                                       │                                │
                                  its Rooms                        its Rooms
                                       │                                │
                                   browsers                         browsers

Every replica publishes to the topic and every replica reads from it, including the one that published — which is what makes a broadcast reach a browser regardless of which replica served the request that caused it.

§Two processes or one

Nothing here needs a second deployment. spawn starts the relay inside the web process, next to the sockets it feeds, so the whole of “broadcasting that works behind a load balancer” is a Kafka topic and one line in a provider.

Structs§

SocketFanOut
Delivers relayed broadcasts to the sockets in a Rooms registry.

Functions§

run
Read the topic and deliver, until something goes wrong.
spawn
Run the relay in the background, reconnecting when it falls over.