node.js - Raising a socket.io event from a Restify handler -
i have resitfy server , socketio server configured per example here. requirement raise socket.io event within handler. so, code looks this:
var restify = require('restify'); var socketio = require('socket.io'); var server = restify.createserver(); server.get('/', function (req, res, next) { res.json(204, null); io.emit("sample", {"sample":"event"}); next(); }); server.listen(8080, function () { console.log('socket.io server listening @ %s', server.url); });
my client code pretty simple , looks this:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>socket.io chat example</title> </head> <body> <script src="socket.io.js"></script> <script type="text/javascript"> var socket = io.connect('http://localhost:8080'); socket.on('sample', function (data) { console.dir(data); }); </script> </body> </html>
when start server, server exists after first time client connects , found in logs:
socket.io:server initializing namespace / +0ms socket.io:server creating engine.io instance opts {"path":"/socket.io"} +3ms socket.io:server attaching client serving req handler +1ms socket.io server listening @ http://0.0.0.0:8080 socket.io:server incoming connection id u9hlzrlbsgfvi8qpaaaa +2s socket.io:client connecting namespace / +4ms socket.io:namespace adding socket nsp / +0ms http.js:690 throw new error('can\'t set headers after sent.'); ^ error: can't set headers after sent. @ serverresponse.outgoingmessage.setheader (http.js:690:11) @ serverresponse.format (/home/vagrant/socket.io-server-example/node_modules/restify/lib/response.js:109:10) @ serverresponse.send (/home/vagrant/socket.io-server-example/node_modules/restify/lib/response.js:231:24) @ emitrouteerror (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:152:13) @ onroute (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:610:21) @ router.find (/home/vagrant/socket.io-server-example/node_modules/restify/lib/router.js:512:5) @ server._route (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:604:21) @ routeandrun (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:572:14) @ server._handle (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:592:9) @ server.onrequest (/home/vagrant/socket.io-server-example/node_modules/restify/lib/server.js:255:14)
what not able understand is, how can serve rest api , socket.io events same port using restify? why restify complain not being able set headers once sent?
am missing fundamental here?
try using using example
pass result listen
method socketio , use io
instance send events on handlers.
var restify = require('restify'); var server = restify.createserver(); var app = server.listen(8080, function() { console.log('%s listening @ %s', server.name, server.url); }); var io = require("socket.io")(app); function respond(req, res, next) { res.send('hello ' + req.params.name); io.emit('sample',{"data":"hello server."}); next(); } server.get('/hello/:name', respond); server.head('/hello/:name', respond); io.on('connection', function(socket){ console.log('new connection'); });
Comments
Post a Comment