From 0396307ec800e4b456772c91eb0b605eabb32c05 Mon Sep 17 00:00:00 2001 From: Marc Juul Date: Wed, 21 Feb 2018 19:00:58 -0800 Subject: [PATCH] Added error handling section to readme --- readme.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/readme.md b/readme.md index e514d11..cae620b 100644 --- a/readme.md +++ b/readme.md @@ -129,6 +129,31 @@ app.ws('/bigdata.json', function(ws, req) { app.listen(3000); ``` +### Error handling + +Errors both from the underlying websocket and from the TCP socket itself are propagated and emitted on the stream so you _must_ provide an error handler for the stream even if you already have an error handler on the websocket, e.g: + +```javascript +var websocket = require('websocket-stream') +var wss = websocket.createServer({server: someHTTPServer}, handle) + +// this is not enough! +someHTTPServer.on('connection', function(socket) { + socket.on('error', function(err) { + console.log("Client socket error:", err); + }); +}); + +function handle(stream, request) { + // we need this as well + stream.on('error', function(err) { + console.log("Stream error:", err); + }); +} +``` + +If you neglect to add the stream error handler then a simple `ECONNRESET` will crash your application. + ## Run the tests ### Server-side tests