Pub/Sub Example? #46
-
Hello, it seems that this lib supports pub/sub but I have not clue how to handle it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hello @johnnytolengo we have some examples in And also in: See the example bellow as used in benchmark, you can use ws.subscribe(topic) and ws.unsusbscribe(topic) and use app.publish(topic, message, opcode) to broadcasting the message to the subscribed clients from socketify import App, AppOptions, OpCode, CompressOptions
remaining_clients = 16
app = App(websocket_factory_max_itens=1_500_000)
def ws_open(ws):
ws.subscribe("room")
global remaining_clients
remaining_clients = remaining_clients - 1
if remaining_clients == 0:
print("All clients connected")
print('Starting benchmark by sending "ready" message')
app.publish("room", "ready", OpCode.TEXT)
def ws_message(ws, message, opcode):
app.publish("room", message, opcode)
def ws_close(ws, close, message):
global remaining_clients
remaining_clients = remaining_clients + 1
app.ws(
"/*",
{
"compression": CompressOptions.DISABLED,
"max_payload_length": 16 * 1024 * 1024,
"idle_timeout": 60,
"open": ws_open,
"message": ws_message,
"close": ws_close,
},
)
app.any("/", lambda res, req: res.end("Nothing to see here!'"))
app.listen(
4001,
lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)),
)
app.run() |
Beta Was this translation helpful? Give feedback.
-
That's fine, now looking for the message dataStructure (protocol) for that pub/sub on both sides client/server. I assume that info is in the uWebsockets documentation including internal api's to get connected clients, stats, etc, etc... Thanks |
Beta Was this translation helpful? Give feedback.
-
It's interesting how the Server handle(eg. more than 1 connection with the same ClientID) the subscriptions(multiple), it supposed to be something very similar to how MQTT brokers do. |
Beta Was this translation helpful? Give feedback.
Hello @johnnytolengo we have some examples in
https://github.com/cirospaciari/socketify.py/tree/main/examples
Chat app example use pub/sub
And also in:
https://github.com/cirospaciari/socketify.py/tree/main/bench/websockets
See the example bellow as used in benchmark, you can use ws.subscribe(topic) and ws.unsusbscribe(topic) and use app.publish(topic, message, opcode) to broadcasting the message to the subscribed clients