Replies: 2 comments 2 replies
-
Hi, and sorry for the delayed reply- had a busy week. In order to have the JS client work it must implement the [RPC protocol(https://github.com/permitio/fastapi_websocket_rpc/blob/master/fastapi_websocket_rpc/rpc_channel.py), which is another logical layer on top of the websocket. The bear minimum here is to implement the right schema for an RPC message and call the pub/sub RPC method publish Which is something like this: {
"request": {
"method": "publish",
"arguments": {"topics":["event"], "data":{"msg": "hello web"} },
"call_id": "<RANDOM-UUID>"
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply, my English is translated by Google. I later flipped through the source code and found publish I have some questions now, but I'm having a hard time expressing in English. I have attached a gif, can you take a look? Why web js subscribe after... Client.py can't receive the message of test.py? Then I used fastapi trigger with no problem. But when I add a web connection. There is no way to receive fastapi trigger messages. After I disconnected the initial web connection, I was able to receive messages. What am I doing wrong? Or am I using the method wrong? server.pyimport asyncio
import uvicorn
from fastapi import FastAPI
from fastapi.routing import APIRouter
from fastapi_websocket_pubsub import PubSubEndpoint
from starlette.websockets import WebSocket
app = FastAPI()
router = APIRouter()
endpoint = PubSubEndpoint(broadcaster='redis://localhost:6379')
@router.websocket('/pubsub')
async def websocket_rpc_endpoint(websocket: WebSocket):
async with endpoint.broadcaster:
await endpoint.main_loop(websocket)
async def events(topic):
await endpoint.publish([topic], {'msg': 'fastapi'})
@app.get('/trigger')
async def trigger_events():
asyncio.create_task(events('event'))
app.include_router(router)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000) client.pyimport asyncio
from fastapi_websocket_pubsub import PubSubClient
async def on_events(data, topic):
print(f"running callback for {data} {topic}!")
async def main():
client = PubSubClient(["event"], callback=on_events)
client.start_client(f"ws://localhost:8000/pubsub")
await client.wait_until_done()
if __name__ == '__main__':
asyncio.run(main()) test.pyimport asyncio
import random
from fastapi_websocket_pubsub import PubSubEndpoint
endpoint = PubSubEndpoint(broadcaster='redis://localhost:6379')
async def run():
while True:
await endpoint.publish('event', {'msg': random.randint(1, 100)})
await asyncio.sleep(1)
if __name__ == '__main__':
asyncio.run(run()) jslet ws = new WebSocket("ws://127.0.0.1:8000/pubsub")
let payload_str = JSON.stringify({request: {method: "subscribe", "arguments": {"topics": ["event"]}}})
ws.onmessage = (event) => {
console.log('get event data', JSON.parse(event.data))
}
ws.send(payload_str) |
Beta Was this translation helpful? Give feedback.
-
Sorry, my native language is not English, my English is poor, if communication is not good I will close issues.
I found the websocket example from #41
Before I found this example, it was not clear that the message should be sent after connecting.
Subscribe Msg
I understand that I can receive messages by sending
Subscribe Msg
.I understand sending messages to topics using Python.
How can I send a message to a topic from Web JS?
Do you have any examples or documentation to look at?
I have a few questions
1.I want to send a message to
Event topic
?2.After the web receives a message once, there are no more messages.
But the web can receive the trigger message.
Beta Was this translation helpful? Give feedback.
All reactions