Express.io VS Express + socket.io有什么实用工具? [英] What utility of express.io VS express + socket.io?
问题描述
我发现socket.io并在此处聊天示例: https://github.com/rauchg/chat-example/blob/master/index.js
I discover socket.io and chat exemple here : https://github.com/rauchg/chat-example/blob/master/index.js
它们直接使用require('express')
和require('socket.io')
.
那
差异,使用的优势:require('express.io')
像这里的 http://express-io.org/一样?
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?
推荐答案
我一直在我的节点应用程序中使用express.io.我发现主要的好处是可以将普通的快速路由与套接字路由混合使用.
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:
在我的应用程序中,我有一个带有Angular客户端的nodejs REST API.我的客户需要显示一些由管理员致电特快专递请求创建的实时通知.
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.
开始时,我以一定的时间间隔查询所有通知,每5秒钟运行一次.
在有几个客户端的情况下,它可以完美工作,但是当客户端增加时,我的服务器就过载了.每个客户端都在请求通知,尽管他们没有新的通知.因此,我决定开始使用socket.io发送实时通知.
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.
如果管理员保存了新通知,则服务器将通过套接字广播该通知.
这里的问题是,管理员在express中调用了普通的POST请求,并且我需要使用socket.io进行广播,因此我集成了express.io,可以将常规的express请求重定向到socket.io方法中进行发射.>
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);
});
这篇关于Express.io VS Express + socket.io有什么实用工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!