This example shows an example of long-polling implemented with BlackSheep.
The JavaScript and front-end part of this example was adopted from: https://javascript.info/long-polling.
- Create a Python virtual environment
- Install depedencies (
pip install blacksheep uvicorn
) - Run the server with
uvicorn server:app
- Open the web site in several browser tabs to see the effect of long-polling
- Submit messages in a browser tab: see how messages are immediately visible in all tabs, thanks to long-polling
- Read the source code in
server.py
to see how long-polling is achieved usingasyncio.Queue
, and howsignal.getsignal(signal.SIGINT)
is used in the@app.on_start
event handler. - The
/subscribe
method is used to subscribe for long-polling. - The
/publish
method is used to publish a message to all subscribers.
To test a client that disconnects, refresh a browser tab, then send a message from an active tab. The console should display messages like this one:
INFO: 127.0.0.1:40834 - "POST /publish HTTP/1.1" 200 OK
🔥🔥🔥 Request is disconnected!
🔥🔥🔥 Request is disconnected!
🔥🔥🔥 Request is disconnected!
🔥🔥🔥 Request is disconnected!
🔥🔥🔥 Request is disconnected!
INFO: 127.0.0.1:40832 - "GET /subscribe?random=0.16177484986012614 HTTP/1.1" 200 OK
INFO: 127.0.0.1:40882 - "GET /subscribe?random=0.08956117949704501 HTTP/1.1" 200 OK
INFO: 127.0.0.1:40882 - "GET /subscribe?random=0.08956117949704501 HTTP/1.1" 200 OK
INFO: 127.0.0.1:40832 - "GET /subscribe?random=0.16177484986012614 HTTP/1.1" 200 OK