-
Notifications
You must be signed in to change notification settings - Fork 40
/
pubsub_broadcaster_server_example.py
52 lines (38 loc) · 1.54 KB
/
pubsub_broadcaster_server_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Multiple Servers linked via broadcaster example.
To run this example.
- 0. Setup a broadcast medium and pass its configuration to the endpoint (e.g. postgres on 'postgres://localhost:5432/' )
- 1. run this script for the servers (as many instances as you'd like) - use the PORT env-variable to run them on different ports
- 2. once the servers are up, run notifier_client_test.py and connect to one of them
- 3. send get request to one server on: '/trigger'
- 4. See that the client recives the event -no matter which server you connected it to, or which server got the initial trigger to publish
"""
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.basename(__file__), "..")))
from fastapi_websocket_pubsub import PubSubEndpoint
import asyncio
import os
from starlette.websockets import WebSocket
import uvicorn
from fastapi import FastAPI
from fastapi.routing import APIRouter
PORT = int(os.environ.get("PORT") or "8000")
app = FastAPI()
router = APIRouter()
endpoint = PubSubEndpoint(broadcaster="postgres://localhost:5432/")
@router.websocket("/pubsub")
async def websocket_rpc_endpoint(websocket: WebSocket):
await endpoint.main_loop(websocket)
app.include_router(router)
async def events():
await asyncio.sleep(1)
await endpoint.publish(["guns", "germs"])
await asyncio.sleep(1)
await endpoint.publish(["germs"])
await asyncio.sleep(1)
await endpoint.publish(["steel"])
@app.get("/trigger")
async def trigger_events():
asyncio.create_task(events())
uvicorn.run(app, host="0.0.0.0", port=PORT)