Loopback with socketio

4,351 views
Skip to first unread message

Matthew Williams

unread,
Jun 25, 2014, 7:56:56 PM6/25/14
to loopb...@googlegroups.com
I would like to use socketio in my loopback server app.  What's the best way to require it.  I am looking for the socket server to run on port 80 along with my loopback server.   Following the socketio example I would use the express app like below:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

Should I add this into the loopback.js file or is there already a configuration I can use with the loopback application to achieve this.

Miroslav Bajtoš

unread,
Jun 26, 2014, 6:37:48 AM6/26/14
to loopb...@googlegroups.com
I'll assume you already have app = loopback(); and you configured all models and middleware.

Now you need to replace the call to app.listen() with the following:

// original code - no socket.io
app.listen()

// new code - add socket.io to the mix
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(app.get('port'), app.get('host'));

Let me know if that works for you.

Miroslav

Raymond Feng

unread,
Jun 26, 2014, 11:07:17 AM6/26/14
to Miroslav Bajtoš, loopb...@googlegroups.com
It could be even simpler:

var server = app.start();
var io = require(‘socket.io’)(server);

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ritchie Martori

unread,
Jun 26, 2014, 12:25:06 PM6/26/14
to Raymond Feng, Miroslav Bajtoš, loopb...@googlegroups.com
If you used `slc lb project` to scaffold your app, in your app.js on line ~ 126, you should be able to reference the `http` Server object by doing this:

```js
app.start = function() {
  var http = app.listen(function() {
    // ...
  });
  require('socketio')(http);
};
```
--

Matthew Williams

unread,
Jun 27, 2014, 6:12:30 AM6/27/14
to loopb...@googlegroups.com
Here is what I did:

var server = http.createServer(app);
 /*
  * Push notifications added
  */
app.io = require('socket.io').listen(server);

I attached io to the app object.  Now I am trying to get a reference to my model instances in a beforeSave hook.  So I can emmit an event with socket i.o.  For example in a product model:

//models/product.js  
Product.beforeSave = function(callback){
console.log("beforeSave");
console.log(callback);
app.io.emmit("productInfo", "my model instance attributes....")
}

How do I reference the model instance from the beforeSave.  I can use afterSave as well I just don't understand how to get the values of the model in the callback.  In the above example the model properties are configured in "models.json".

Thanks.

Matthew Williams

unread,
Jun 27, 2014, 6:20:16 AM6/27/14
to loopb...@googlegroups.com
Okay I see the context of the callback is the model instance.  I just use the keyword this to get my model attributes:  this.price etc...


On Wednesday, June 25, 2014 6:56:56 PM UTC-5, Matthew Williams wrote:

Charles Soetan

unread,
Sep 19, 2014, 4:19:39 AM9/19/14
to loopb...@googlegroups.com
in my server.js I did this

if (require.main === module) {
  // app.start();
  var server = require('socket.io')(app.start()); // I replace the app.start() with this
}

How do I now use the socket in my boot folder.

Thanks

Miroslav Bajtoš

unread,
Sep 19, 2014, 12:50:56 PM9/19/14
to Charles Soetan, loopb...@googlegroups.com
On Fri, Sep 19, 2014 at 1:19 AM, Charles Soetan <caso...@gmail.com> wrote:
in my server.js I did this

if (require.main === module) {
  // app.start();
  var server = require('socket.io')(app.start()); // I replace the app.start() with this
}

I'd say you have to store your socket.io server on the app object, as was shown in earlier in this thread.

if (require.main === module) {
  app.io = require('socket.io')(app.start());
}

Then you can use `app.io` to access the socket.io server.

Note that the socket.io server is not available at the time your `boot/*.js` are run.

Miroslav

Charles Soetan

unread,
Sep 22, 2014, 1:51:24 AM9/22/14
to loopb...@googlegroups.com, caso...@gmail.com
thanks.

jmls

unread,
Sep 29, 2014, 7:27:30 AM9/29/14
to loopb...@googlegroups.com, caso...@gmail.com
does anyone have an example of how to use socket.io in loopback ?

I have followed all the above examples, it seems to be available, but whenever I try to connect from a client I just get a "disconencted" message

so. my code looks like this

if (require.main === module) {
  app.io = require('socket.io')(app.start());

where would / should I put the 

io.on('connection'

?

thanks

Alexander Makarenko

unread,
Oct 6, 2014, 6:50:47 PM10/6/14
to loopb...@googlegroups.com, caso...@gmail.com
Why not add boot script with something like following:

// boot/io.js
module.exports = function(app) {
  app.on('started', function() {
    require('./io')(app, app.io);
  });
}

// boot/io/index.js
module.exports = function(app, io) {
  require('./chat')(app, io);
}

// boot/io/chat.js
module.exports = function(app, io) {
  io.on('connection', function(socket) {
    // functionality HERE
  });

Alexander Makarenko

unread,
Oct 6, 2014, 8:38:52 PM10/6/14
to loopb...@googlegroups.com
Sorry for mistake in code: require('./io')(app, app.io) may not work because io.js is already required.

--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/6qpQwggf2_c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Sincerely,
Alexander Makarenko

Curriculum vitae: http://liberitas.info/cv
Skype: estliberitas

Miroslav Bajtoš

unread,
Oct 7, 2014, 7:39:44 AM10/7/14
to Alexander Makarenko, loopb...@googlegroups.com, Charles Soetan
On Tue, Oct 7, 2014 at 12:50 AM, Alexander Makarenko <estlib...@gmail.com> wrote:
Why not add boot script with something like following:

// boot/io.js
module.exports = function(app) {
  app.on('started', function() {
    require('./io')(app, app.io);
  });
}

To my best knowledge, this will not work, because `app` is a request handler function, but `socket.io` requires an instance of `http.Server` instead.

Miroslav 

Alexander Makarenko

unread,
Oct 7, 2014, 8:06:37 AM10/7/14
to loopb...@googlegroups.com, estlib...@gmail.com, caso...@gmail.com
You are right. I posted wrong code. Here is working one:

// server/server.js
...
if (require.main === module) {
  app.server = app.start()
}

// boot/io.js
module.exports = function(app) {
  app.on('started', function() {
    app.io = require('socket.io').listen(app.server)
    require('./io')(app, app.io);
  });
}

jmls

unread,
Oct 24, 2014, 6:27:20 AM10/24/14
to loopb...@googlegroups.com, estlib...@gmail.com, caso...@gmail.com
thanks for this - it works ... but .. ;)

how do I refer to app.io in my model ? I am wanting to emit a message on an update, so I am hooking into the afterSave()


module.exports = function (Salesrep) {
 Salesrep.afterSave = function(next) {
     app.io.to("data:salesrep").emit( "afterUpdate", { body: this } )
   }

however, obviously, app is not defined

Julian

Miroslav Bajtoš

unread,
Oct 24, 2014, 6:37:33 AM10/24/14
to jmls, loopb...@googlegroups.com, Alexander Makarenko, Charles Soetan
On Fri, Oct 24, 2014 at 12:27 PM, jmls <jul...@dotr.com> wrote:
thanks for this - it works ... but .. ;)

how do I refer to app.io in my model ? I am wanting to emit a message on an update, so I am hooking into the afterSave()


module.exports = function (Salesrep) {
 Salesrep.afterSave = function(next) {
     app.io.to("data:salesrep").emit( "afterUpdate", { body: this } )
   }

module.exports = function (Salesrep) {
 Salesrep.afterSave = function(next) {
   Salesrep.app.io.to("data:salesrep").emit( "afterUpdate", { body: this });
   next();
 };
}

See [1] and [2] for more details.

Don't forget to call `next` in your custom method.

Miroslav

Alexander Makarenko

unread,
Oct 24, 2014, 6:39:40 AM10/24/14
to loopb...@googlegroups.com, estlib...@gmail.com, caso...@gmail.com
Just read docs. For example, here: Migrating apps to version 2.0

So you'll find application object is available as app property of model:

And your example will look like...

module.exports = function (Salesrep) {
  Salesrep.afterSave = function(next) {
    Salesrep.app.io.to("data:salesrep").emit( "afterUpdate", { body: this } )
  }
  ...
}

Or if you want to use access Salesrep.app as variable, you have to setup model inside model's attached listener:

module.exports = function (Salesrep) {
 Salesrep.on('attached', function() {
   var app = Salesrep;

   Salesrep.afterSave = function(next) {
     app.io.to("data:salesrep").emit( "afterUpdate", { body: this } )
   }
   }
   ...
 })
}

jmls

unread,
Oct 24, 2014, 8:04:57 AM10/24/14
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
Thanks a lot Miroslav, that seems to work for me.

however, even if I get a list of connected clients to the room (sending to  [ X3Es6HDM9U61ItstAAAB: true ]
) , they never receive the message

console.log("sending to ",Salesrep.app.io.sockets.adapter.rooms["data:customer"]);
     
Salesrep.app.io.sockets.to("data:customer").emit( "data")

i've tried 


.emit and .send ;)

jmls

unread,
Oct 30, 2014, 9:31:01 AM10/30/14
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
fwiw, this works for me

{model}.app.io.sockets.in("toRoom").emit("someMessage",{yourMessageBody})

julian

Kevin Sproles

unread,
Dec 1, 2014, 6:53:48 PM12/1/14
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
For me, doing this worked:


  app.io = require('socket.io')(app.start());

But in my hooks (i.e. .afterCreate or .afterUpdate) I can't get any of the examples from this thread to work. I instead used:

Salesrep.app.io.emit( "salesrep:save", this)

Felipe Figueroa

unread,
Dec 4, 2014, 7:07:00 AM12/4/14
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
Doing this

app.io = require('socket.io')(app.start());

would lead  for socket io to answer instead of loopback, or it wouldl listen on another port?

I'm confused about what will happen to the default endpoints and verbs if you run socket.io along with loopback.

Maker Su

unread,
Jan 8, 2015, 7:20:33 AM1/8/15
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
Here is my example, https://github.com/makersu/loopbackchat

Felipe Figueroa於 2014年12月4日星期四UTC+8下午8時07分00秒寫道:

Felix Itzenplitz

unread,
Oct 26, 2015, 9:45:33 AM10/26/15
to loopb...@googlegroups.com, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
Hi, this is my implementation of integrating socket.io into loopback:


It allows you to separate all socket.io stuff into a .js file in your boot/ folder.

Felipe Figueroa

unread,
Oct 28, 2015, 6:57:57 PM10/28/15
to LoopbackJS, jul...@dotr.com, estlib...@gmail.com, caso...@gmail.com
It makes every bit of sense. However, I'm getting error 400. I believe I should add upgrade headers through nginx, but can't seem to get it right.
Reply all
Reply to author
Forward
0 new messages