Naveen Paul
Naveen Paul

Reputation: 454

Using Socket IO to emit messages when a Node JS process is completed

How can use Socket IO and Express + Nodejs such that on user login, I'm running a MongoDb update query in the background on a MEAN stack app. I have to notify the user that the MongoDb process has been completed in a non blocking way. I don't want to use callbacks as I have to redirect the user to the landing page without him waiting for this query to complete. So that if this MongoDb process completes after 30 seconds after the user has logged-in, I want to flash a message that the query updates have been done.

I have setup Socket IO but I don't know how to use it for my particular situation.

Express + Node code

io.on('connection', function(client) {
    console.log('Client connected...');
    client.on('complete', function(data) {

        client.emit('messages', 'Hello from server');
    });
});

Below is my AngularJs code.

var socket = io.connect('/socketTest');
 socket.on('connect', function(data) {
     socket.emit('complete', 'Mongo Process Done');
 });

These are examples that I copied from a website and I really don't understand what needs to be done so that I can catch this event on Client side.

Upvotes: 2

Views: 1637

Answers (3)

Themasterhimself
Themasterhimself

Reputation: 1016

I think this is the solution you are looking for WRT to server code of socketio in nodejs.

https://dev.to/wakeupmh/how-to-decouples-emit-events-from-connection-event-into-socket-io-8dk

Upvotes: 0

sebenalern
sebenalern

Reputation: 2561

Following off of @aseferov answer.

You can also you use btford's angular-socket-io library

This makes it a lot easier to use socket io the angular way.

For example the factory method is just:

Simple Example: In most cases, adding the following to your app should suffice:

factory('socket', function (socketFactory) {
  return socketFactory();
});

Also one functionality that helped me in a SPA chat app was the ability to use the socket.forward. The reason why this helped is because in a single page application every time you switch pages a new client connection is made but the old one is never disconnected so if I wanted to send a message it was printed in the message box for every client connection.

When using the socket.forward method this avoids duplicating event handlers when a user navigates back and forth between routes so I will not have duplicate methods printed out when sending a message.

Example:

socket.forward('someEvent', $scope);
$scope.$on('socket:someEvent', function (ev, data) {
      $scope.theData = data;
});

Upvotes: 1

aseferov
aseferov

Reputation: 6393

if you want catch client.emit('messages', 'Hello from server'); you can simply do this

var socket = io.connect('/socketTest');
 socket.on('connect', function(data) {
     socket.emit('complete', 'Mongo Process Done');
 });

 socket.on('messages', function(data) {
     console.log(data) //'Hello from server'
 });

you can create socket factory

app.factory('socket', function ($rootScope) {
var socket = io.connect();
return {
    on: function (eventName, callback) {
        socket.on(eventName, function () {
            var args = arguments;
            $rootScope.$apply(function () {
                callback.apply(socket, args);
            });
        });
    },
    emit: function (eventName, data, callback) {
        socket.emit(eventName, data, function () {
            var args = arguments;
            $rootScope.$apply(function () {
                if (callback) {
                    callback.apply(socket, args);
                }
            });
        })
    }
};

and use it every controller like

function MyCtrl($scope, socket) {
    socket.on('message', function(data) {
        ...
    });
};

Upvotes: 3

Related Questions