简体   繁体   中英

NodeJS server no http for Redis subscription

I want to develop a process that only subscribes to a Redis channel and stay alive to handle received messages.

I wrote the following code:

var redis = require("redis");
var sub = redis.createClient({host: process.env.REDIS_HOST});

console.log('subscribing...')
sub.on('subscribe', () => console.log('subscribed'));
sub.on('message', (ch, msg) => console.log(`Received message on ${ch}:${msg}`));
console.log('done')

But obviously it does not work: when launched it goes through all lines and dies. I think I don't need framework like Express because my process does not use http.

How could I write a server that stay alive "forever" without using http frameworks?

I used the exact code above and the process stays open. In the code above you aren't publishing any messages, therefore you will only see "subscribing..." and "done" printed to the terminal.

Also, as mentioned you aren't subscribing to the channel either.

您没有订阅频道:

sub.subscribe('channel');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM