@streamerson/gateway-wss
v0.0.1
Published
> A WebSocket Redis Stream Gateway
Readme
streamerson-gateway-wss
A WebSocket Redis Stream Gateway
This package provides an out-of-the-box solution for proxying traffic between users and consumers via WebSockets. The server will provide an HTTP(s) endpoint from which it can UPGRADE to a websocket after authentication. This allows for a direct event-driven flow between client and server.
Table of Contents
Installation
- yarn:
yarn add @streamerson/gateway-wss - npm:
npm install @streamerson/gateway-wss
Usage
Create a WSS Gateway proxying Websocket connections to a Redis stream:
import {WebSocketServer} from '@streamerson/gateway-wss';
import {Topic} from "@streamerson/core";
export enum Events {
HELLO_EVENT = 'hello'
}
export const streamTopic = new Topic('my-stream-topic');
const wssServer = new WebSocketServer({
port: 3000
});
await wssServer.streamRoute('/hello', Events.HELLO_EVENT, streamTopic, {
authenticate: () => {
return true;
}
});
await wssServer.listen();(See: app-websockets example)).
This server will create an instance of a websocket server (driven by uWebSockets.js), which will listen for incoming socket traffic and write it to a corresponding stream as a producer. When an application listens on that stream (see: Consumer or Consumer Groups), the data will be transferred back to the websocket-server and written to the corresponding websocket for the originating user.
Essentially, this server acts as a proxy between a websocket client and a stream, allowing the client to send and receive messages to/from a consumer.
import {StreamConsumer} from '@streamerson/consumer';
import {Events, streamTopic} from "./streamerson-gateway";
const consumer = new StreamConsumer({
eventMap: {
[Events.HELLO_EVENT]: (e) => {
return {
world: 'I am a stream processor'
};
}
},
topic: streamTopic
});
await consumer.connectAndListen();
