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?
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With