node-restify
node-restify copied to clipboard
Using sockjs / websocket with restify?
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' });
I have the same problem
same
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' });