Skip to content

Is there a detailed tutorial on "websocket"? #46

Description

@hgalytoby

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

{
   request:  {
       method: "subscribe",
       "arguments": {
       "topics": ["event"]
       }
   }
}

I understand that I can receive messages by sending Subscribe Msg.

I understand sending messages to topics using Python.

endpoint = PubSubEndpoint(broadcaster="redis://localhost:6379")
await endpoint.publish(['event'], {'msg': 'ok'})

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?

let ws = new WebSocket("ws://localhost:8080/pubsub")
ws.send(JSON.stringify(????))

2.After the web receives a message once, there are no more messages.

import asyncio
from fastapi_websocket_pubsub import PubSubEndpoint


async def main():
    endpoint = PubSubEndpoint(broadcaster='redis://localhost:6379')
    while True:
        await endpoint.publish(["event"], data={'msg': 'hello web'})
        await asyncio.sleep(1)


if __name__ == '__main__':
    asyncio.run(main())

But the web can receive the trigger message.

async def events():
    await endpoint.publish(['event'], {"msg": "fastapi"})


@app.get('/trigger')
async def trigger_events():
    asyncio.create_task(events())

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions