当前位置:网站首页>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 .
边栏推荐
- AS如何不区分大小写去进行智能提示
- 取组合数问题
- 屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
- Rxdart is used instead of stateful in fluent
- Camera连拍自动化测试shell脚本
- About the pictures inserted in the word document, only the following part is displayed
- Learn RX programming from me -- concat
- Framework customization series (VI) -- shield fallbackhome mobile phone from pop-up during startup and directly enter the launcher
- NTC,PT100热电阻转4-20mA温度信号转换器
- 0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8
猜你喜欢

FTP file transfer protocol

如何有效进行回顾会议(上)?

PXE网络装机

What is the concept of game testing? What are the test methods and processes?

1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151

PXE network installation

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

Baidu proposes a dynamic self distillation method to realize dense paragraph retrieval by combining interactive model and double tower model

软件架构与设计(十)-----架构技术

MLX90640 红外热成像仪测温传感器模块开发笔记(八)
随机推荐
Software architecture and design (I) -- key principles
占空比开关量输出高速脉冲计数器RTUModbus模块IBF63
一波骚操作解决Laya场景编辑器报错问题
Knowledge points qwer
H265 streaming on OBS
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 (VIII) -- distributed architecture
语音社交系统——完善有声系统产业链
Return the two subscripts of the array according to the input target.
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
Getting started with crawlers (1) -- requests (1)
便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法
1200 times faster! MIT develops a new generation of drug research and development AI, and suspends the old model
Learn RX programming from me -- concat
Rongyun real-time community solution
屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
Summary and arrangement of postgraduate entrance examination information of 985 colleges and universities nationwide
Problem of fetching combinatorial numbers
Thermistor PT100, NTC to 0-10v/4-20ma converter
Several slips of X rust, those things that have to be said