Open 0cv opened 8 years ago
The ctx.socket
you're seeing in the Koa middleware is not connected to web sockets, its to do with Koa itself.
koa-socket tries to leave koa alone, rather than add functionality specifically to koa it works alongside, but shares the same server for convenience. The only things that the attach
method does are to share the server and to attach the io instance/s directly to your Koa application.
As the socket.io instance is attached to the Koa instance you can access some global io methods, such as broadcast
, from the app instance but in order to emit to a specific connection you'd have to add some functionality to your code to add the connection id when you hit routes, that way you can emit to a specific connection (pretty sure socket.io allows this). This requires that you pass through your connection id when you hit routes from the client, otherwise Koa has no way to know which WS connection is associated with the client hitting the route.
hello ,would you answer my issue?
I'm trying to integrate socket.io into existing routes/middleware but when doing ctx.socket.emit from the standard Koa middleware, the client does not receive the message. I must also say that ctx.socket.id is undefined, even so ctx.socket isn't. Is it something not supported, or am I doing anything wrong?
Explained differently, this is what I'm trying to do: 1- Client starts an Ajax request (POST/GET/...) 2- Koa handles the request and answers in a regular way 3- Additionally this triggers something on the middleware so that the server send messages to the client, but this is where this does not work.
I'm using Koa 2, Koa-Router 7, if that's any relevant.