redis NPM 官方介绍地址:https://www.npmjs.com/package/redis
let redis = require('redis'); let subscriber; let publisher; let options = { host: "192.168.211.33", port: 6379, password: "123456" } subscriber = redis.createClient(options); //创建订阅客户端 subscriber.on("subscribe", (channel, count) => { console.log("Subscribe # subscribed to " + channel + ", " + count + " total subscriptions"); }); subscriber.on('message', (channel, message) => { console.log('Message # Channel ' + channel + ': ' + message); }); subscriber.on('unsubscribe', (channel, count) => { console.log("Unsubscribe # to " + channel + ", " + count + " total subscriptions"); }); subscriber.on("error", (err) => { console.log("redis error: " + JSON.stringify(err)); }); subscriber.subscribe('main_chat_room'); //订阅信道 publisher = redis.createClient(options); //创建发布客户端 publisher.publish('main_chat_room', "BBQ IS HERE"); //发布消息 setTimeout(() => { subscriber.unsubscribe('main_chat_room'); //延时取消信道订阅 }, 15000); setTimeout(() => { subscriber.end();//关闭连接 publisher.end(); }, 20000);