Re: how to use socket.io in different routes of expressjs

3,282 views
Skip to first unread message

Arnout Kazemier

unread,
Nov 5, 2012, 8:25:04 AM11/5/12
to sock...@googlegroups.com
If you want to blow up your server with pointless memory leaks, that would be the way to go yes.. You add a listener every time that route is hit..

On Monday 5 November 2012 at 14:22, Pablo Villoslada Puigcerber wrote:

You can use it as middleware:

var socketHandler = function(req, res, next) {
  io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    
    socket.on('disconnect', function() { 
      console.log('A socket with id ' + socket.id + ' disconnected');
    });
  });

  next(); // Passing the request to the next handler in the stack.
}

app.get('/', routes.index);
app.get('/another', socketHandler, another.index);

Cheers, Pablo.

On Friday, September 7, 2012 1:45:48 PM UTC+2, Marshall wrote:
hello, i'm a nodejs newbie and i really need you guys' help!
currently, i'm using socket.io with expressjs together and all the examples i saw through internet are using socket.io in the app.js where server is created. but what i want to do is just using socket.io in one sub path of root directory.(another js file in routes directory)
i thought maybe i can pass the http server object to my route file or call socket.io in middleware but i didn't do them successfully. 
so can you tell me a simple and straight-forward way to do the thing like that?
thank you guys in advance.

Anand George

unread,
Nov 6, 2012, 5:22:15 AM11/6/12
to sock...@googlegroups.com
Hi Mike,

Nice. Two small errors:
1. The readme should have 
cp config.ex.js config.js instead of cp config.js.ex config.js

2. Running on Node 0.6.19 throws an error

if (fs.existsSync(config.appPath+'/mast/templates')) {
        ^
TypeError: Object #<Object> has no method 'existsSync'

Works fine with 0.8.0.

Anand

On Tue, Nov 6, 2012 at 2:24 AM, Mike McNeil <michael....@gmail.com> wrote:
Our MVC framework, Sails, was designed for this purpose.  It enforces authentication rules and routes Socket.io messages automatically to the appropriate Express route.

Nicolas Chambrier

unread,
Nov 8, 2012, 6:50:39 AM11/8/12
to socket_io
The websocket server is completely different than your HTTP server.
OK it occurs to be listening to same port, but that's only for your convenience, and you should consider there are two separate servers running.

Therefore, attaching socket.io server to a "route" of your http server does not make sense.

You will simply use or not use websocket features from your page (client-side), that's all :)


On 7 September 2012 13:45, Marshall <zhen...@gmail.com> wrote:
hello, i'm a nodejs newbie and i really need you guys' help!
currently, i'm using socket.io with expressjs together and all the examples i saw through internet are using socket.io in the app.js where server is created. but what i want to do is just using socket.io in one sub path of root directory.(another js file in routes directory)
i thought maybe i can pass the http server object to my route file or call socket.io in middleware but i didn't do them successfully. 
so can you tell me a simple and straight-forward way to do the thing like that?
thank you guys in advance.



--
Nicolas Chambrier, aka naholyr

Blog : http://naholyr.fr
Formateur Clever Institut : http://clever-institut.com/formateur/nicolas-chambrier


Reply all
Reply to author
Forward
0 new messages