@6river/loopback-component-changestreamer 中文文档教程
loopback-component-changestreamer
该组件观察一些指定的模型并通过 SSE 通知更改。
与 Loopback /change-stream 通道的主要区别在于,此实现只为每个模型创建两个观察者(保存后和删除后),然后将更改流式传输到保持活动状态的注册连接。 相反,Loopback 为每个 连接创建了两个相同的观察者。
Install and Setup
安装公司: npm install --save @6river/loopback-component-changestreamer
重要! 像这样在 middleware.json 文件中禁用压缩中间件:
{
...
"compression": {
"enabled":false
},
...
}
将以下配置添加到 component-config.json: 以
{
...
"loopback-component-changestreamer": {
"mountPath": "/api/updates",
"reconnectTimeout": 3000,
"responseTimeout": 120000,
"models": [
"Foo",
"Bar",
"Baz"
]
},
...
}
e2e/ 目录为例了解如何进行项目配置。
配置参数:
- mountPath - base URL to subscribe for updates;
- reconnectTimeout - instruct Browser to reconnect after this timeout if connection is lost;
- responseTimeout - the response socket will be closed after this timeout;
- models - array of model names to observe.
上面的组件配置添加了 3 个中间件规则:
- GET "/api/updates" to connect some SourceEvent listener;
- GET "/api/updates/stat" to see a statistics about number of current connections;
- DELETE "/api/updates" to close all registered connections.
下面的代码片段可以在客户端使用来连接:
var src = new EventSource('//<host>:<port>/api/updates');
src.addEventListener('message', function(message) {
...
});
loopback-component-changestreamer
The component observes a number specified models and notifies about the changes by SSE.
The main difference with Loopback /change-stream channels is that this implementation creates only two observers (after save and after delete) per model and then streams the changes to keep-alive registered connections. In contrast Loopback creates two same observers for each connection.
Install and Setup
Install the company: npm install --save @6river/loopback-component-changestreamer
Important! Disable compression middleware in middleware.json files like this:
{
...
"compression": {
"enabled":false
},
...
}
Add the following configuration to component-config.json:
{
...
"loopback-component-changestreamer": {
"mountPath": "/api/updates",
"reconnectTimeout": 3000,
"responseTimeout": 120000,
"models": [
"Foo",
"Bar",
"Baz"
]
},
...
}
See e2e/ directory as an example for how to make project configuration.
The configuration parameters:
- mountPath - base URL to subscribe for updates;
- reconnectTimeout - instruct Browser to reconnect after this timeout if connection is lost;
- responseTimeout - the response socket will be closed after this timeout;
- models - array of model names to observe.
The component configuration above adds 3 middleware rules:
- GET "/api/updates" to connect some SourceEvent listener;
- GET "/api/updates/stat" to see a statistics about number of current connections;
- DELETE "/api/updates" to close all registered connections.
The following snippet can be used on client side to connect:
var src = new EventSource('//<host>:<port>/api/updates');
src.addEventListener('message', function(message) {
...
});