ui5-middleware-websocket
v3.1.6
Published
UI5 middleware utilities to enable WebSockets for Express servers like the UI5 server
Downloads
294
Readme
UI5 middleware to enable websockets
:wave: This is an open‑source, community‑driven project, developed and actively monitored by members of the UI5 community. You are welcome to use it, report issues, contribute enhancements, and support others in the community.
Middleware for ui5-server to demo WebSocket usage with an simple echo service.
Prerequisites
- Requires at least
@ui5/[email protected](to supportspecVersion: "3.0")
:warning: UI5 CLI Compatibility All releases of this UI5 CLI extension using the major version
3require UI5 CLI V3. Any previous releases below major version3(if available) also support older versions of the UI5 CLI. But the usage of the latest UI5 CLI is strongly recommended!
Install
npm install ui5-middleware-websocket --save-devUsage
- Define the dependency in
$yourapp/package.json:
"devDependencies": {
// ...
"ui5-middleware-websocket-echo": "*"
// ...
}- configure it in
$yourapp/ui5.yaml:
server:
customMiddleware:
- name: ui5-middleware-websocket-echo
afterMiddleware: compression
mountPath: /wsechoHow it works
The middleware uses the utility function websocket which allows to inject a WebSocket middleware function into a UI5 middleware function:
const websocket = require("ui5-middleware-websocket/lib/websocket");
/**
* WebSocket middleware to act as an echo service.
*
* @param {object} parameters Parameters
* @param {@ui5/logger/Logger} parameters.log Logger instance
* @returns {Function} Middleware function to use
*/
module.exports = ({ log }) => {
return websocket(function echo(ws, req /*, next */) {
ws.on("message", function (message) {
log.info(`message: ${message}`);
ws.send(`echo ${message}`);
});
log.info(`Connection established with ${req.url}`);
});
};License
This work is dual-licensed under Apache 2.0 and the Derived Beer-ware License. The official license will be Apache 2.0 but finally you can choose between one of them if you use this work.
When you like this stuff, buy @pmuessig a coke when you see him.
