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

documentation of koa support #1334

Closed
1 task done
kmathmann opened this issue Mar 22, 2018 · 4 comments
Closed
1 task done

documentation of koa support #1334

kmathmann opened this issue Mar 22, 2018 · 4 comments

Comments

@kmathmann
Copy link

  • I've searched for any related issues and avoided creating a duplicate issue.

Description

The usage of ws with express is documented in the readme but the usage of ws with koa is not documented.

The only information that i get if i search for "ws koa" or "ws koa2" are third party npm packages and the old issue #868.

But instead of using third party npm packages it's possible to use ws with koa2 like it is possible with express.

const app = new Koa();
const server = http.createServer(app.callback());

Can we add the documentation of koa2 support?

Attachments:

const Koa = require('koa');
const http = require('http');
const url = require('url');
const WebSocket = require('ws');

const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

const server = http.createServer(app.callback());
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws, req) {
  const location = url.parse(req.url, true);
  // You might use location.query.access_token to authenticate or share sessions
  // or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312)

  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

server.listen(8080, function listening() {
  console.log('Listening on %d', server.address().port);
});
@lpinca
Copy link
Member

lpinca commented Mar 22, 2018

The only difference is app.callback(), isn't it better to only document that instead of copying the whole example?

@lpinca
Copy link
Member

lpinca commented Mar 22, 2018

Also if we do it for koa we open the doors to any other framework out there. I think the example should only show how to use an externally created server without focusing on any particular framework.

@kmathmann
Copy link
Author

Yes your argumentation makes sense. Adding examples for each framework isn't a solution.
Instead we should change the ExpressJS example to an general example how to use ws next to frameworks such as express, koa and others.

@lpinca
Copy link
Member

lpinca commented Mar 22, 2018

Feel free to open a PR.

lpinca added a commit that referenced this issue Apr 7, 2018
lpinca added a commit that referenced this issue Apr 7, 2018
@lpinca lpinca closed this as completed in c801e99 Apr 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants