Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone get koa-websocket to work with koa-router@7.0.1? #281

Closed
ralyodio opened this issue Jun 18, 2016 · 9 comments
Closed

Can anyone get koa-websocket to work with koa-router@7.0.1? #281

ralyodio opened this issue Jun 18, 2016 · 9 comments
Labels

Comments

@ralyodio
Copy link

ralyodio commented Jun 18, 2016

koa-websocket works fine with koa-router@5.x but not 7.

kudos/koa-websocket#14

If anyone can point me in the right direction of how to fix this library I can create a PR.

I think it has to do with this line but I'm not sure how to fix it for 7.x

https://github.com/kudos/koa-websocket/blob/master/index.js#L27

@ralyodio
Copy link
Author

bump

@cymen
Copy link

cymen commented Sep 29, 2016

I ran into this too. I haven't had a chance to dig into it yet but switching to koa-route for the websocket route made it work (although not sure if that is actually a solution for my usage, just did it to debug which part wasn't working).

@ralyodio
Copy link
Author

Can you paste an example of how you worked around this?

@cymen
Copy link

cymen commented Sep 29, 2016

@chovy I adapted this example from the koa-websocket README:

const koa = require('koa'),
  route = require('koa-route'),
  websockify = require('koa-websocket');

const app = websockify(koa());

// Note it's app.ws.use and not app.use 
app.ws.use(route.all('/test/:id', function* (next) {
  // `this` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object. 
  // the websocket is added to the context on `this.websocket`. 
  this.websocket.send('Hello World');
  this.websocket.on('message', function(message) {
    // do something with the message from client 
    console.log(message);
  });
  // yielding `next` will pass the context (this) on to the next ws middleware 
  yield next;
}));

app.listen(3000);

That uses koa-route instead of koa-router. That made websockets work from my local HTTP server.

So it was just pin-pointing that koa-router appears to be the breaker. I'm just getting started with websockets and Koa so I might have it wrong but I was hoping that if koa-router worked, I'd be able to get the current logged in user and such (ie in my other normal routes, ctx.req.user). Maybe I can get koa-route to hook into that too (but if already using koa-router, would prefer to use it of course).

My test code is on an uncommitted branch that I can't access right now but I can post a better example this evening (PST).

@ralyodio
Copy link
Author

oh, it uses generators not async/await? that sucks.

@jbielick
Copy link
Collaborator

jbielick commented Oct 1, 2016

Is koa-websocket written for koa 2? Koa 2 middleware signatures have changed and so has koa-compose. Is koa-websocket is compatible with koa2 in general?

@ralyodio
Copy link
Author

ralyodio commented Oct 1, 2016

@jbielick it was originally for koa@1.x but i don't think support for 2.x would be that difficult. I linked to the line I think is the problem.

@cymen
Copy link

cymen commented Oct 1, 2016

I opened a PR over at koa-websocket for a next as @jbielick points out it definitely looks like signature change.

kudos/koa-websocket#18

@jbielick
Copy link
Collaborator

jbielick commented Oct 1, 2016

@cymen nice! Great stuff. Closing this comment as it can be tracked at kudos/koa-websocket#14 or kudos/koa-websocket#18

@jbielick jbielick closed this as completed Oct 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants