Where Can I found WS api documentation ?
Hello I would like to know if it's possible to fill "origin" in the WS event. Or find a way to add identity information about the sender. Or get ip address of the sender.
Where Can I find ws API documentation Hono documentation ?
13 Replies
Do you want to get this information when starting the connection or on each message?
ideally I would like to have it on opening and on message.
For now, I had an identifier in each messages, and I keep track of the opened socket in an array, updating this array on a close or an open event. I need that last thing for broadcasting.
something like that:
in fact I realized that “ws” passed to each handler call was an instance specific to each client
Is there anything I've missed that makes this possible?
You do have the context object in
upgradeWebSocket
, so you can access all the information regarding the reqIf I console log the context I don't see req and If I try to read it I get an error. This is what I tried:
And what I get :
Maybe I could try the set/get in order too put a specific id on each ws, however...
yet req is mentioned in the doc ... I don't understand... https://hono.dev/docs/api/context#req
Context - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Seems to be working on my end, I am using worker's runtime. Can you share a reproduction?
ah ok I see, it works like this but not from the onMessage handler apparently
If you want to reproduce :
console.log(c.req)
throw en error when called from "onClose" handler
I don't know if it's expected...
Anyway there is no information about the remote. I'm not even sure it's possible with websockets.
unless a referrer field is added to the headers
I don't really need it that much anyway...anyway if you need to reproduce the code is here https://codeberg.org/Mik000/nammu-web-api/src/branch/master/broker/broker.ts but i guess it's just not possible with ws
Codeberg.org
nammu-web-api/broker/broker.ts at master
Maybe this could be related to deno, can you try this with workers? You can refer to my code as an example if you are not familiar with workers, the API is similar
Ok I will do it
Hello @Aditya Mathur . Just had a look to workers. As I understand it's related to cloudflare and for serverless application . But it turns out that the clients of the broker relies on a sqlite backend which is incompatible with a serverless application, I am afraid .
the "clients" use the broker here https://codeberg.org/Mik000/nammu-web-api/src/commit/cb3c60999e6eec54001b6b3959686f06b6fd3f86/server.ts#L108 and in other places in the future. (each time one of them handles a successful POST to be exact)
That said I could use cloudflare just for the broker and Durable Objects for persistence. But it takes me a bit away from what I wanted to do in the first place. (Anyway run serverless function on cloudflare looks very interesting, so thank you to give me the opportunity to dig this subject)