当前位置:网站首页>Data real-time feedback technology
Data real-time feedback technology
2022-07-28 15:52:00 【I'm not a code God】
In fact, I don't know how to start this title , This is a scene like this , In the development of background management system , Especially when real-time monitoring system , It is often necessary to show the continuous update and change of data . The common technology is polling , Or use websocket Long connection real-time communication . We know webpack There is a hot update function in debugging mode , It is achieved through the server data push function . It's called Server-Sent Events(SSE).
For specific principles and usage, please refer to Ruan Yifeng The article
http://www.ruanyifeng.com/blog/2017/05/server-sent_events.html
This article will combine Node.js、SSE、Koa、Pm2、Rxjs Technology to achieve an elegant development technology of real-time data feedback . stay Node.js End , We can install one NPM package :http-event-stream To do server push . The source code of this package is very simple , Readers can have a look for themselves , It's not difficult to write by yourself . up to now , Start from the server to the browser , Real time data update is very simple , But we still need the final development experience , That is, how to make the data on the server real-time “ push ” To with http-event-stream In your request ?
A simple way , That is, when you get from the client SSE On request , Start a timer , Get the data in the database or memory in the timer , And then send it to the client . Before writing this code , We need to prepare a middleware to use Rxjs The event of is converted into SSE Send out .
const { createEventStream } = require("http-event-stream")
const { Sink } = require('fastrx')
module.exports = async function(ctx, next) {
const sink = new Sink
const stream = createEventStream(ctx.res, {
onClose() {
sink.dispose()
}
})
sink.next = data => stream.sendMessage({ event: 'message', data: JSON.stringify(data) })
sink.complete = err => stream.close();
sink.subscribe(await next())
ctx.respond = false
}there fastrx library , It's my own high-speed Rxjs Realization , You can go NPM Check it out on the website . I have also written articles on relevant principles in detail . With this middleware , Suppose we need to start from MongoDB Every 5 Read data once per second . our Controller The response function can be written like this (koa-router)
async function facade({collection }) {
return pipe(interval(5000), startWith(0), switchMap(x => fromPromise(collection('apps').aggregate([{
$lookup: {
from: "servers",
as: "servers",
let: { app: "$_id" },
pipeline: [
{ $match: { $expr: { $eq: ["$app", "$$app"] } } },
{ $group: { _id: "$env", envs: { $push: "$$ROOT" } } },
{ $project: { k: "$_id", v: "$envs", _id: 0 } }
]
}
}, { $addFields: { servers: { $arrayToObject: "$servers" } } }]).toArray())))
}among collection Is from Mongodb Of client Ahead of time Koa Encapsulation in .
Advanced
There are many limitations in obtaining data regularly , In the real scene , We often need to broadcast data to the monitoring front desk in time when an event occurs , And some data is not stored somewhere for you to obtain . Then I need to establish a data source to Koa The pipe in the middle of the controller . Here is the introduction pm2 Managed interprocess communication . This method can also be extended to network communication .
const subject_rounds = rx.subject()
pm2.launchBus((err, bus) => bus.on('myEvent', ({ data, process }) => subject_rounds.next(data)))combination koa-router
router.get('/getData', eventStreamM, ctx => subject_rounds)among eventStreamM Middleware mentioned before
The browser side can use EventSource The object is listening for data . In the process of another data source , We use it process.send({type:"myEvent",data: data }) Broadcast events to pm2 Just go to the event bus .
For other system architectures , We can use different technologies to broadcast data , Such as message queue . But it can be used in the end Rxjs Medium subject As a bridge to SSE Push Events .Subject It is this kind of existence , Act as both observer and observable object , about SSE It is an observable object , For other data sources, they are observers . If more than one person opens SSE monitor , Can perfectly deal with .
边栏推荐
- 在OBS上进行H265推流
- Rust 入门指南(rustup, cargo)
- 数牍 X Rust,那些不得不说的事
- Framework定制系列(六)-----屏蔽FallbackHome手机启动中弹窗直接进入Launcher
- VirturalBox解决kernel driver问题
- Docker容器实现MySQL主从复制
- DNS domain name resolution protocol
- Monkey stress test
- Preparing for listing in the United States? Arm announced that it would divest the Internet of things service business: the future will focus on the underlying chip design
- Perception of life
猜你喜欢

【微信小程序开发(七)】订阅消息

How as makes intelligent prompts regardless of case

Software architecture and design (VIII) -- distributed architecture
![[delete specified number leetcode]](/img/16/b40492d8414a363a3a24f00b4afd47.png)
[delete specified number leetcode]

Shell programming specifications and variables

以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165

MLX90640 红外热成像仪测温传感器模块开发笔记(八)

About the pictures inserted in the word document, only the following part is displayed

Minimum heap improves the efficiency of each sort

Principle and application of low cost / small volume module rs485/232 to analog signal ibf33
随机推荐
使用systemd管理服务
多用型混合信号8AI/4DI/DO转串口RS485/232MODBUS采集模块IBF30
Youdao cloud notes remove the bottom advertisement
Learn RX programming from me -- concat
ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
About the pictures inserted in the word document, only the following part is displayed
Duty cycle switch output high speed pulse counter rtumodbus module ibf63
分体式测斜探头安装要点及注意事项
What are the process specifications of software testing? How to do it?
Matlab does not overwrite importing Excel
PXE网络装机
如何压缩与解压缩ramdisk.img
德国电信否认加强与华为合作,并称过去3年已缩减与华为的合作
Daily news on July 28, 2022: Science: AI has made another breakthrough in protein design, and can design specific functional proteins
Framework定制系列(六)-----屏蔽FallbackHome手机启动中弹窗直接进入Launcher
管理区解耦架构见过吗?能帮客户搞定大难题的
Samba Server Setup Guide
The price war of off screen fingerprints has resumed, and second-line manufacturers are expected to win 30% of the market this year?
Software architecture and design (V) -- data centric architecture