Similar to message queues like RabbitMQ, this broker broadcasts notifications to all connected clients.
The server uses []byte
as data type, to handle any type(images, strings).
The broker manages multiple clients using dedicated channels for each. For every new client that connects, the broker creates a unique chan []byte for that client.
- Manages client connections using channels and a map.
Notifier
: Channel for broadcasting events to all clients.newClients
&closingClients
: Channels to handle registration and removal of clients.clients
: A map to track active clients.
Broker may handle delegation of resource management and cleanup, ensuring smooth client registration and deregistration, even in cases where clients disconnect abruptly.
That's why we have clients
, newClients
, closingClients
as seaparte entities in the struct.
Each client has its own unique channel for receiving messages. Using separate channels ensures:
- Message Isolation: Messages are routed to specific clients.
- No Overlap: Clients only receive messages meant for them.
- Concurrency Control: Each client's connection is handled independently, avoiding messy disconnections or errors that could affect others.
If a shared channel was used for all clients, several issues could arise:
- Message Overlap: All clients would receive every message, even if not relevant.
- Lack of Control: There would be no way to target individual clients.
- Concurrency Issues: Disconnection of one client could disrupt the entire system. Broker Responsibilities
To run the server
go run .
For each new client run, we can see the client id being printed as well.
curl -N http://localhost:3000