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

Socket.IO #717

Closed
chdow opened this Issue Dec 4, 2014 · 11 comments

Comments

Projects
None yet
@chdow
Copy link

chdow commented Dec 4, 2014

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(){});
@theolampert

This comment has been minimized.

Copy link

theolampert commented Dec 8, 2014

@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);
@earthday

This comment has been minimized.

Copy link

earthday commented Jan 10, 2015

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.

@hertzg

This comment has been minimized.

Copy link

hertzg commented Jan 15, 2015

@earthday 👍 Can confirm.

@meteormatt

This comment has been minimized.

Copy link

meteormatt commented Apr 17, 2015

@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);
});

@micahr micahr added this to the Backlog milestone Jun 17, 2015

@micahr

This comment has been minimized.

Copy link
Member

micahr commented Jun 17, 2015

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.

@mike-aungsan

This comment has been minimized.

Copy link

mike-aungsan commented Aug 6, 2015

It took me a few hours

@feiyuw

This comment has been minimized.

Copy link

feiyuw commented Nov 27, 2015

I got this issue too, thanks @theolampert

@bizMD

This comment has been minimized.

Copy link

bizMD commented Jan 17, 2016

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

mgwalker added a commit to mgwalker/retros that referenced this issue Jul 17, 2016

@MichaelBuhler

This comment has been minimized.

Copy link

MichaelBuhler commented Feb 25, 2017

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.

@moroine

This comment has been minimized.

Copy link

moroine commented Apr 24, 2017

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

@retrohacker

This comment has been minimized.

Copy link
Member

retrohacker commented Apr 29, 2017

Closing as resolved in #669

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

retrohacker added a commit that referenced this issue Apr 29, 2017

Create FEATURE_REQUESTS.md
Closes:

#289
#381
#474
#575
#790
#633
#717
#576
#576
#909
#875
#860
#853
#850
#829
#813
#801
#921
#1101
#1019
#989
#632
#708
#737
#859
#1326
#1327
#927
#1099
#1068
#1040
#1035
#957
#948
#1134
#1136
#1183
#1206
#1286
#1323

> Note: this issue closes _but does not resolve_ the issues listed, we are just tracking them in another medium
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment