node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

Using sockjs / websocket with restify?

Open dexcell opened this issue 13 years ago • 3 comments

Hello, Does anyone make sockjs / websocket working with restify?

Tried code below, but it doesn't work. When i open http://localhost:8000/echo (which is the websocket url) restify will handle the routing, thus returning error not found.

var restify = require('restify'),
    sockjs = require('sockjs');

var socketServer = sockjs.createServer();

socketServer.on('connection', function(conn) {
    conn.on('data', function(message) {
        console.log('connection on data', message);
        conn.write(message);
    });

    conn.on('close', function() {
        console.log('connection closed');
    })
});

var server = restify.createServer();

server.get('/hello/:name', function(req, res, next) {
    res.send('hello ' + req.params.name);
    return next();
});

server.listen(8000, function() {
    console.log('%s listening at %s', server.name, server.url);
});

socketServer.installHandlers(server, { prefix: '/echo' });

dexcell avatar Oct 11 '13 05:10 dexcell

I have the same problem

0xbboyd avatar Oct 15 '13 19:10 0xbboyd

same

jedahan avatar Nov 09 '13 18:11 jedahan

You want to bind the socket handler to the http server not the restify server. The following works for me.

var server = restify.createServer();
var socketServer = sockjs.createServer();
socketServer.installHandlers(server.server , { prefix: '/echo' });

kangaroo5383 avatar Dec 24 '13 23:12 kangaroo5383