JSPM

loopback-component-changestreamer

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q42524F
  • License ISC

Stream model changes by SSE

Package Exports

  • loopback-component-changestreamer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (loopback-component-changestreamer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 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"
    ],
    "headers": [
      "x-auth-request-user"
    ]
  },
  ...
}

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.
    • headers - headers and values to embed in the stream metadata.

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) {
  ...
});