Node-restify: Socket.IO

Created on 4 Dec 2014  路  11Comments  路  Source: restify/node-restify

I was having problems with socket.io when connecting, so I went to the example code in the API guide. I get the same error:

socket.io server listening at http://0.0.0.0:8080

http.js:689
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.format (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/response.js:95:10)
    at ServerResponse.send (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/response.js:205:24)
    at emitRouteError (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:150:13)
    at onRoute (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:616:17)
    at Router.find (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/router.js:505:5)
    at Server._route (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:610:17)
    at routeAndRun (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:569:14)
    at Server._handle (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:589:9)
    at Server.onRequest (/Users/dow/code/august/lockopnotifier/node_modules/restify/lib/server.js:253:14)

I _think_ I'm doing this right, and the code is essentially the same as the example in the docs.

Here's my server:

var restify = require('restify');
var socketio = require('socket.io');

var server = restify.createServer();
var io = socketio.listen(server);

server.get('/', function indexHTML(req, res, next) {
    next();
});

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
            console.log(data);
    });
});

server.listen(8080, function () {
    console.log('socket.io server listening at %s', server.url);
});

Here's my client:

var io = require('socket.io-client');
var socket = io('http://localhost:8080');

socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});

Most helpful comment

@chdow This is referenced here #575 and here #669, workaround for the moment appears to be to bind your socket client to (server.server). In your case go:

var server = restify.createServer();
var io = socketio.listen(server.server);

All 11 comments

@chdow This is referenced here #575 and here #669, workaround for the moment appears to be to bind your socket client to (server.server). In your case go:

var server = restify.createServer();
var io = socketio.listen(server.server);

I also meet this issue on version 2.8.4, 2.8.3 and 2.8.2, but it works when I rollback to version 2.8.1. BTW, my socket.io version is 1.2.1.

This error happens when requesting /socket.io/socket.io.js.

@earthday :+1: Can confirm.

@theolampert Yes. this works.
@chow The code below works.

var restify = require('restify');
var socketio = require('socket.io');

var server = restify.createServer();
var io = socketio.listen(server.server);

server.get('/', function indexHTML(req, res, next) {
    next();
});

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

server.listen(8080, function () {
    console.log('socket.io server listening at %s', server.url);
});

Looks like we need a better solution for allowing requests to opt in / out of some handlers that happen for every request. Hardcoding of socket.io and other web socket based solutions doesn't scale. I'll add this and the other Engine.io issue #575 to the backlog.

It took me a few hours

I got this issue too, thanks @theolampert

Thank you for the workaround, this took me a while to find.

this was solved by #669. you should pass socketio: true into restify.createServer() so that restify does not attempt to handle the request after socket.io has already handled it.

However, the solution was never added to the documentation, so I just wasted 6 hours on this.

I think the doc need to be updated to tell users that socketio option should be set to true.

Closing as resolved in #669

Will make note of the documentation request (creating a FEATURE_REQUESTS.md document as we speak).

Was this page helpful?
0 / 5 - 0 ratings