Skip to content

Added error handling section to readme #140

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the block that should not be needed, not the other one. Why is this needed?


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
Expand Down