Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What utility of express.io VS express + socket.io?

I discover socket.io and chat exemple here : https://github.com/rauchg/chat-example/blob/master/index.js

They use directly require('express') AND require('socket.io').

So what the differences, the advantages, to use : require('express.io') like here http://express-io.org/ ?

It's just to win one line? Seriously? or there is another layer with new tools?

like image 874
Matrix Avatar asked Oct 04 '15 17:10

Matrix


1 Answers

I have been using express.io in my node app. I found that the principal advantage is that you can mix the normal express routes with socket routes.

Let me explain a real example:

In my app I have a nodejs REST API with an Angular clients. My clients need to show some real time notifications that were created by an administrator calling an express post request.

At the beginning I put a time interval in angular to consult all the notifications, running it every 5 seconds.
With a few clients it works perfect but when clients increased my server was overloaded. Each client was requesting notifications despite them not having new notifications. So I decided to start using socket.io to send real time notifications.

If my administrator saves a new notification, the server broadcasts the notification through the socket.
The problem here was that the administrator calls a normal POST request in express and I needed to broadcast using socket.io, so I integrate express.io and I can redirect normal express request to a socket.io method to do an emit.

var express = require('express.io');
var app = express();

app.http().io()

app.post('/notificacion', function(req, res){
//I save the notification on my db and then ...
req.io.route('enviar');
});

app.io.route('enviar', function(req) { 
    app.io.room('personas').broadcast('alertasControlador',req.io.request.data.notificacion);
});
like image 101
Kevin Sanchez Avatar answered Nov 15 '22 08:11

Kevin Sanchez