1- # ws: a node.js websocket library
1+ # ws: a node.js WebSocket library
22
33[ ![ Version npm] ( https://img.shields.io/npm/v/ws.svg )] ( https://www.npmjs.com/package/ws )
44[ ![ Linux Build] ( https://img.shields.io/travis/websockets/ws/master.svg )] ( https://travis-ci.org/websockets/ws )
@@ -16,7 +16,7 @@ for the full reports.
1616* ** HyBi drafts 07-12** (Use the option ` protocolVersion: 8 ` )
1717* ** HyBi drafts 13-17** (Current default, alternatively option ` protocolVersion: 13 ` )
1818
19- ### Installing
19+ ## Installing
2020
2121```
2222npm install --save ws
@@ -39,6 +39,46 @@ compiler is installed on the host system.
3939 validation. But if you want to be 100% spec-conforming and have fast
4040 validation of UTF-8 then this module is a must.
4141
42+ ## API Docs
43+
44+ See [ ` /doc/ws.md ` ] ( https://github.com/websockets/ws/blob/master/doc/ws.md )
45+ for Node.js-like docs for the ws classes.
46+
47+ ## WebSocket compression
48+
49+ ` ws ` supports the [ permessage-deflate extension] [ permessage-deflate ] extension
50+ which enables the client and server to negotiate a compression algorithm and
51+ its parameters, and then selectively apply it to the data payloads of each
52+ WebSocket message.
53+
54+ The extension is enabled by default but adds a significant overhead in terms of
55+ performance and memory comsumption. We suggest to use WebSocket compression
56+ only if it is really needed.
57+
58+ To disable the extension you can set the ` perMessageDeflate ` option to ` false ` .
59+ On the server:
60+
61+ ``` js
62+ const WebSocket = require (' ws' );
63+
64+ const wss = new WebSocket.Server ({
65+ perMessageDeflate: false ,
66+ port: 8080
67+ });
68+ ```
69+
70+ On the client:
71+
72+ ``` js
73+ const WebSocket = require (' ws' );
74+
75+ const ws = new WebSocket (' ws://www.host.com/path' , {
76+ perMessageDeflate: false
77+ });
78+ ```
79+
80+ ## Usage examples
81+
4282### Sending and receiving text data
4383
4484``` js
@@ -90,41 +130,7 @@ wss.on('connection', function connection(ws) {
90130});
91131```
92132
93- ### ExpressJS example
94-
95- ``` js
96- const express = require (' express' );
97- const http = require (' http' );
98- const url = require (' url' );
99- const WebSocket = require (' ws' );
100-
101- const app = express ();
102-
103- app .use (function (req , res ) {
104- res .send ({ msg: " hello" });
105- });
106-
107- const server = http .createServer (app);
108- const wss = new WebSocket.Server ({ server });
109-
110- wss .on (' connection' , function connection (ws ) {
111- const location = url .parse (ws .upgradeReq .url , true );
112- // You might use location.query.access_token to authenticate or share sessions
113- // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
114-
115- ws .on (' message' , function incoming (message ) {
116- console .log (' received: %s' , message);
117- });
118-
119- ws .send (' something' );
120- });
121-
122- server .listen (8080 , function listening () {
123- console .log (' Listening on %d' , server .address ().port );
124- });
125- ```
126-
127- ### Server sending broadcast data
133+ ### Broadcast example
128134
129135``` js
130136const WebSocket = require (' ws' );
@@ -152,25 +158,38 @@ wss.on('connection', function connection(ws) {
152158});
153159```
154160
155- ### Error handling best practices
161+ ### ExpressJS example
156162
157163``` js
158- // If the WebSocket is closed before the following send is attempted
159- ws .send (' something' );
164+ const express = require (' express' );
165+ const http = require (' http' );
166+ const url = require (' url' );
167+ const WebSocket = require (' ws' );
160168
161- // Errors (both immediate and async write errors) can be detected in an optional
162- // callback. The callback is also the only way of being notified that data has
163- // actually been sent.
164- ws .send (' something' , function ack (error ) {
165- // If error is not defined, the send has been completed, otherwise the error
166- // object will indicate what failed.
169+ const app = express ();
170+
171+ app .use (function (req , res ) {
172+ res .send ({ msg: " hello" });
167173});
168174
169- // Immediate errors can also be handled with `try...catch`, but **note** that
170- // since sends are inherently asynchronous, socket write failures will *not* be
171- // captured when this technique is used.
172- try { ws .send (' something' ); }
173- catch (e) { /* handle error */ }
175+ const server = http .createServer (app);
176+ const wss = new WebSocket.Server ({ server });
177+
178+ wss .on (' connection' , function connection (ws ) {
179+ const location = url .parse (ws .upgradeReq .url , true );
180+ // You might use location.query.access_token to authenticate or share sessions
181+ // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
182+
183+ ws .on (' message' , function incoming (message ) {
184+ console .log (' received: %s' , message);
185+ });
186+
187+ ws .send (' something' );
188+ });
189+
190+ server .listen (8080 , function listening () {
191+ console .log (' Listening on %d' , server .address ().port );
192+ });
174193```
175194
176195### echo.websocket.org demo
@@ -207,10 +226,26 @@ examples folder.
207226
208227Otherwise, see the test cases.
209228
210- ## API Docs
229+ ## Error handling best practices
211230
212- See [ ` /doc/ws.md ` ] ( https://github.com/websockets/ws/blob/master/doc/ws.md )
213- for Node.js-like docs for the ws classes.
231+ ``` js
232+ // If the WebSocket is closed before the following send is attempted
233+ ws .send (' something' );
234+
235+ // Errors (both immediate and async write errors) can be detected in an optional
236+ // callback. The callback is also the only way of being notified that data has
237+ // actually been sent.
238+ ws .send (' something' , function ack (error ) {
239+ // If error is not defined, the send has been completed, otherwise the error
240+ // object will indicate what failed.
241+ });
242+
243+ // Immediate errors can also be handled with `try...catch`, but **note** that
244+ // since sends are inherently asynchronous, socket write failures will *not* be
245+ // captured when this technique is used.
246+ try { ws .send (' something' ); }
247+ catch (e) { /* handle error */ }
248+ ```
214249
215250## Changelog
216251
@@ -222,3 +257,4 @@ for changelog entries.
222257[ MIT] ( LICENSE )
223258
224259[ archive ] : http://web.archive.org/web/20130314230536/http://hobbycoding.posterous.com/the-fastest-websocket-module-for-nodejs
260+ [ permessage-deflate ] : https://tools.ietf.org/html/rfc7692
0 commit comments