LocomotiveJS and Socket.IO

1,047 views
Skip to first unread message

Rui

unread,
Apr 13, 2012, 1:29:08 PM4/13/12
to locomo...@googlegroups.com
Hi again

Been playing around with SocketIO and trying to get it to work with Locomotive.
I succeeded in doing this, but its kind of an hack.

What is the best way to achieve this?


File - all.js
<code>
module.exports = function() {
    .....
    require('socket.io').listen(this._routes._http).sockets.on('connection', function (socket) {
        console.log("Connection");
      socket.emit('news', { hello: 'world' });
    });
}
</code>

Best regards

Jared Hanson

unread,
Apr 13, 2012, 1:46:24 PM4/13/12
to locomo...@googlegroups.com
Thanks for pushing the envelope on these early Locomotive versions.  

I'd recommend checking out the master branch, which is where v0.2.0 is being worked on, and doing development with that (npm link it, if you wish).  One of my goals for that is to cleanly expose a standard Express instance so socket.io and the like can be layered on easily.

When doing that, you wouldn't use the built-in lcm command line to start the server, but rather write your own short myserver.js file, containing something like the following:

myserver.js

    var locomotive = require('locomotive');

    // Boot Locomotive.  Loads routes, controllers, etc from the file system
    // and calls back with a fully configured express instance.
    locomotive.boot('./path/to/app', 'development', function(err, express) {
      if (err) { throw err; }

      // Hookup socket.io.  Any other module that runs on top of HTTP could
      // be configured here too.
      require('socket.io').listen(express);
  
      // start Express listening for requests
      express.listen(port, address, function() {
        var addr = this.address();
        debug('listening on %s:%d', addr.address, addr.port);
      });
    });

Thats it!  Now you can just run node myserver.js, and Locomotive will load up your MVC web app and hand you an Express instance for further customization and layering, and you can listen when ready.

I think its going to be pretty slick.  Let me know what you think.

Jared

Duncan Wong

unread,
Apr 29, 2012, 1:09:37 PM4/29/12
to locomo...@googlegroups.com
I see the rationale.  I like the transparency and customization, but not the boilerplate.  Would a default file be auto-generated from the lcm command?

Jared Hanson

unread,
Apr 29, 2012, 1:27:48 PM4/29/12
to locomo...@googlegroups.com
Yeah, that's an interesting idea.  For most MVC-only apps, I figure the default `lcm server` command is sufficient, and there's no need to further customize the boot process.

It'd be nice to define some conventions surrounding this, though.  Perhaps a "config/listeners" directory (not sure the best name), that contains scripts that attach other protocols (Socket.io, DNode, etc.) to Express.  I'll mull it over.  Let us know if you have any ideas.

Jared
--
Jared Hanson <http://jaredhanson.net/>

Denis Wolf

unread,
Oct 4, 2012, 11:11:41 AM10/4/12
to locomo...@googlegroups.com
Hi, Jared

It has been a while since 0.2 and now those 'hacks' don't work thx to the changes in Express3, I assume.
Is there a simple method to connect socket.io v.9 to locomotive 3.1 ?)

Marcus Koosmann

unread,
Jan 21, 2013, 1:36:52 AM1/21/13
to locomo...@googlegroups.com
I'm also having trouble using these techniques to get SocketIO running on Locomotive 0.3 (on Express 3).  Is there any way to get it working with these versions?

Thanks,
-Marcus

Denis Wolf

unread,
Jan 21, 2013, 4:07:26 AM1/21/13
to locomo...@googlegroups.com
In the end I went for a 'dirty way' and required it as separate module than processed IO as another high-level component and required it's connection altogether with model in controller.

particularly at:

it's not the best way to handle things and I run socket.io on separate port, but it works good enough.

Roland Flanagan

unread,
Mar 26, 2013, 9:26:24 PM3/26/13
to locomo...@googlegroups.com
To get around this, I used this to bind it to express 3.0

server.listen(port, address, function() {
    io.listen(this);
    var addr = this.address();
    console.log('listening on %s:%d', addr.address, addr.port);
});
Reply all
Reply to author
Forward
0 new messages