This is a simple socket broadcasting server. It is written in Python 3.9.
It uses the socket.io
module to create a TCP socket server.
It uses the threading
module to create a thread for each client that connects to the server.
The server broadcasts event
with data
to all connected clients whitin a given channel
.
The server also runs a Fastapi server to retrieve the event
, data
and channel
to broadcast to.
- Python 3.9
- venv
- Docker
- Docker-compose
- Clone the repository
- Run
cd Folder
to go into the folder - Run
pyton3 -m venv venv
to create a new virtual environment - Run
source venv/bin/activate
to activate the virtual environment for unix like systems - Run
pip install -r requirements.txt
to install the required packages
- Clone the repository
- Run
cd Folder
to go into the folder - Run docker-compose up -d --build to build and run the docker container
The server runs a Fastapi server to receive the event
, data
and channel
to broadcast to.
the endpoint is http://localhost:8000/broadcast/{channel}/{event}
and it accepts a POST
request with the payload like:
JSON Payload:
{
"data": "data_to_broadcast"
}
String Payload:
"data_to_broadcast"
Integer Payload:
123
Float Payload:
123.45
Array Payload:
["string", 123, 123.45, {"data": 12}]
When sending a False
payload, the server will return a 422
error.
but the booleans true
and false
are valid payloads.
The server runs a socket server on the following endpoint http://localhost:8000/socket.io
.
The server uses the socket.io
module to create the socket server.
to subscribe to a channel, you need to emit the subscribe
event with the channel name as the payload.
to unsubscribe from a channel, you need to emit the unsubscribe
event with the channel name as the payload.
to receive a broadcast, you need to listen to the event
name that you want to receive.
the server will broadcast the event
with the data
to all connected clients within the given channel
.
- Open Postman
- Create a new request
- Select the
Websocket Request
method (currently beta) - Select
Socket.io
as the protocol - Enter the following url
http://localhost:8000
- In Settings tab, select client_version as
4.0
- In Events tab, add event names to listen to
- Click on
Connect
- In the
Messages
tab, you enter a channel name as your message - on the bottom right, check
Ack
and entersubscribe
as the event name - Click on
Send
You now subscribed to the channel and you will receive all broadcasts to that channel.