How to access io in BootScript

26 views
Skip to first unread message

Rajkaran Chauhan

unread,
Jun 23, 2017, 2:25:50 PM6/23/17
to LoopbackJS
I am on loopback 3.0  

Want to access app.io object in boot script or if possible socket object. I want to create events in boot script instead of server.js.

Is it possible to do?

Raj

Raymond Feng

unread,
Jun 23, 2017, 3:39:33 PM6/23/17
to LoopbackJS
The boot script can have an optional callback argument, for example:

module.export = function(app, callback) {
 // …
 // DO I/O
}
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/b308485f-73c6-4889-8c91-27d031c55994%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rajkaran Chauhan

unread,
Jun 26, 2017, 11:48:52 AM6/26/17
to LoopbackJS
what input output I will be doing in async boot script. What I have read so far is app object is same for both type of boot scripts.  Can you provide me a code snippet.

Francois Laforge

unread,
Jun 27, 2017, 9:50:49 AM6/27/17
to LoopbackJS
The I/O is exactly what Raymond wrote.  Your boot script will have one of the two signatures:

moodule.exports = function(app){
 
// code goes here
 
return;  // don't forget to return because it's syncronous
}

module.exports = function(app, callback){
 
// same code goes here
  callback
(err); // callback has an error param in case of error
}
Message has been deleted

Rajkaran Chauhan

unread,
Jul 6, 2017, 9:58:55 AM7/6/17
to LoopbackJS
I have solved the problem by following this thread https://groups.google.com/forum/#!topic/loopbackjs/6qpQwggf2_c

All I was missing is
app.on('started', function() {});

 I am posting my code here for those who are looking for same answer.

server.js

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

boot/realtime.js

module.exports = function(app) {

    app.on('started', function() {
        console.log(app.io)

        app.io.on('connection', function(socket){
            console.log('a user connected');
            var received = ['welcome to the chat'];
            socket.emit('message', { message: received });

            socket.on('send', function (data) {
                received.push(data.message);
                console.log('received', received)
                socket.emit('message', {message : received});
            });

            socket.on('disconnect', function(){
                console.log('user disconnected');
            });
        });
    });
}


Reply all
Reply to author
Forward
0 new messages