Node.js 将redis订阅消息中继到websocket

Node.js 将redis订阅消息中继到websocket,node.js,websocket,redis,publish-subscribe,Node.js,Websocket,Redis,Publish Subscribe,使用每分钟接收数据的模式(*:*:*)订阅redis;还作为websocket服务器运行,检查特定订阅消息,如果特定订阅消息的数据由redis返回,则直接将从redis返回的数据推送到该WS通道 例如:如果WS-client想要订阅一个名为Binance:BTC-USDT:1m的频道,并且该数据每分钟都来自redis,那么我如何在从redis获得新数据后立即将其发送到WS-client 实现这一点最有效的方法是什么 export {}; const redis = require("r

使用每分钟接收数据的模式
(*:*:*)
订阅redis;还作为websocket服务器运行,检查特定订阅消息,如果特定订阅消息的数据由redis返回,则直接将从redis返回的数据推送到该WS通道

例如:如果WS-client想要订阅一个名为Binance:BTC-USDT:1m的频道,并且该数据每分钟都来自redis,那么我如何在从redis获得新数据后立即将其发送到WS-client

实现这一点最有效的方法是什么

export {};
const redis = require("redis");
const WebSocket = require("ws");

const subscriber = redis.createClient();
subscriber.psubscribe("*:*:*");
const wss = new WebSocket.Server({ port: 8080 });

subscriber.on("pmessage", function (pattern, channel, message) {
  console.log(message);
});

wss.on("connection", function connection(ws) {
  ws.on("message", function incoming(message) {
    console.log("Server Received: %s", message);
  });

  ws.send("something from server");
});

ws.ts

索引

export {};
const redis = require("redis");
const WebSocket = require("ws");
import { IWSClient } from "./types/IChannel";
import WSHandler from "./ws";

const subscriber = redis.createClient();
subscriber.psubscribe("*:*:*");
const wss = new WebSocket.Server({ port: 8080 });

var wsHandler = new WSHandler();

subscriber.on("pmessage", function (
  pattern: any,
  channel: string,
  message: string
) {
  wsHandler.publishToChannel(channel, message);
});

wss.on("connection", function connection(ws: IWSClient) {
  ws.on("message", function incoming(_subMessages: string) {
    let subMessages: Array<string> = JSON.parse(_subMessages);
    console.log("Server Received: ", subMessages);
    subMessages.forEach((channel) => wsHandler.bindChannelWS(channel, ws));
  });

  ws.send(JSON.stringify({ connection: "Initiated" }));
});
export{};
const redis=需要(“redis”);
const WebSocket=require(“ws”);
从“/types/IChannel”导入{IWSClient}”;
从“/ws”导入WSHandler;
const subscriber=redis.createClient();
subscriber.psubscribe(“*:*:*:*”);
const wss=newwebsocket.Server({port:8080});
var wsHandler=new wsHandler();
订阅服务器.on(“pmessage”,函数(
图案:任何,
频道:字符串,
消息:string
) {
wsHandler.publishToChannel(通道,消息);
});
on(“连接”,函数连接(ws:IWSClient){
打开(“消息”,函数传入(_子消息:字符串){
let子消息:Array=JSON.parse(_子消息);
日志(“服务器接收:”,子消息);
forEach((channel)=>wsHandler.bindChannelWS(channel,ws));
});
send(JSON.stringify({connection:“Initiated”}));
});
export {};
const redis = require("redis");
const WebSocket = require("ws");
import { IWSClient } from "./types/IChannel";
import WSHandler from "./ws";

const subscriber = redis.createClient();
subscriber.psubscribe("*:*:*");
const wss = new WebSocket.Server({ port: 8080 });

var wsHandler = new WSHandler();

subscriber.on("pmessage", function (
  pattern: any,
  channel: string,
  message: string
) {
  wsHandler.publishToChannel(channel, message);
});

wss.on("connection", function connection(ws: IWSClient) {
  ws.on("message", function incoming(_subMessages: string) {
    let subMessages: Array<string> = JSON.parse(_subMessages);
    console.log("Server Received: ", subMessages);
    subMessages.forEach((channel) => wsHandler.bindChannelWS(channel, ws));
  });

  ws.send(JSON.stringify({ connection: "Initiated" }));
});