UNPKG

3.15 kBMarkdownView Raw
1# koa-websocket
2
3[![Circle CI](https://circleci.com/gh/kudos/koa-websocket.svg?style=svg)](https://circleci.com/gh/kudos/koa-websocket)
4
5> This is for Koa 1, if you're looking for Koa/Koa-route 2 compatibility see the `next` branch.
6
7## Installation
8
9`npm install koa-websocket`
10
11## Usage
12
13Server:
14```js
15const koa = require('koa'),
16 route = require('koa-route'),
17 websockify = require('koa-websocket');
18
19const app = websockify(koa());
20
21/**
22 * We define a route handler for when a new connection is made.
23 * The endpoint can be anything you want the client to connect to.
24 * If our WebSocket server address is `ws://localhost:3000` and our route is `/`,
25 * this will handle any new WebSocket connections to `ws://localhost:3000/`.
26 * If our route was `/test`, the handler would fire only when
27 * a connection was made to `ws://localhost:3000/test`.
28 */
29app.ws.use(route.all('/', function* (next) {
30 /**
31 *`this` refers to the context in the `app.ws` instance, not `app`. `app` and `app.ws` use separate middleware/contexts.
32 * to access a middleware's context here, you must pass the middleware to `app.ws.use()`.
33 */
34
35 // the websocket is added to the context as `this.websocket`.
36 this.websocket.on('message', function(message) {
37 // print message from the client
38 console.log(message);
39 });
40
41 // send a message to our client
42 this.websocket.send('Hello Client!');
43
44 // yielding `next` will pass the context (this) on to the next ws middleware
45 yield next;
46}));
47
48app.listen(3000);
49
50```
51
52Client:
53Here is a simple client using the WebSockets API, which is built-in to the browser. (Make sure you check that your browser version supports it.)
54
55```js
56/**
57 * See "Writing WebSocket client applications" on MDN for more:
58 * https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
59 */
60const socket = new WebSocket('ws://localhost:3000/');
61socket.onopen = function (event) {
62 // send a message to the server
63 socket.send('Hello server!');
64};
65
66socket.onmessage = function (event) {
67 // print message from server
68 console.log(event.data);
69};
70```
71
72## Sharing middleware between Koa and WebSockets
73To use middleware in WebSockets, the middleware must be passed in to `app.ws.use()`. Passing your middleware only to `app.use()` will not make it available in WebSockets. You can share your middleware between Koa and WebSockets by passing an instance of the middleware to both `app.use()` and `app.ws.use()`.
74
75The following example shows how to share a session store with WebSockets.
76
77### Example: Sharing a session store
78```js
79const koa = require('koa'),
80 route = require('koa-route'),
81 session = require('koa-session'),
82 websockify = require('koa-websocket');
83
84const app = websockify(koa());
85
86var sessionStore = session(app);
87
88// pass the session store middleware to both the app and app.ws
89// to share it between Koa and websockets
90app.use(sessionStore);
91app.ws.use(sessionStore);
92
93app.ws.use(route.all('/', function* (next) {
94 // `this` context now contains `this.session`
95 console.log(this.session); // prints the Koa session object
96 yield next;
97}));
98
99app.listen(3000);
100
101```