slact / nchan

Fast, horizontally scalable, multiprocess pub/sub queuing server and proxy for HTTP, long-polling, Websockets and EventSource (SSE), powered by Nginx.
https://nchan.io/
Other
3.01k stars 291 forks source link

nchan <=> redis pub/sub? #258

Open hellochenwang opened 8 years ago

hellochenwang commented 8 years ago

Hi Leo, would it be useful/possible if nchan publishes and subscribes to redis pub/sub channels? essentially nchan provides a websocket interface to redis pub/sub.

slact commented 8 years ago

You're not the first person to ask about this. It's entirely possible, and actually not hard to implement. My question for you is why do you want this feature? What problems would this help you solve?

hellochenwang commented 8 years ago

I'm building an IoT project, redis pubsub is the messaging bus/IPC. I am planning to build a web interface that can have access to the messaging bus.

There are some nodejs implementations to do this, but I think nchan would have much better performance.

slact commented 8 years ago

Ok, I understand.

The biggest challenge for me with this feature is how to design its configuration. There are some things that cannot be avoided which will make it quirky at best, and unintuitive at worst. Most importantly, this feature will need to be configured on subscriber locations, which is weird. It would look something like this:

http {

   server {
      listen 80;

      location ~ /subscribe/(.*)$ {
        nchan_subscriber;
        nchan_channel_id $1;
        nchan_upstream_message_source /publish/$1;
      }

      location ~ /publish/(.*)$ {
        internal;
        nchan_channel_id $1;
        nchan_publisher redis-pubsub;
        nchan_redis_url "redis://localhost:6379/0";
        nchan_publisher_redis_pubsub_key "channel_$1";
      }

   } 
}

Is that configuration you'd understand just by looking at it?

hellochenwang commented 8 years ago

nchan_publisher redis-pubsub; This line makes me think all the nchan_channel_ids would be mapped to redis pubsub channel ids.

nchan_publisher_redis_pubsub_key "channel_$1"; This line makes me think nchan_channelid "$1" maps to redis pubsub channel "channel$1".

They both make sense to me. The 2nd one seems more flexible.

hellochenwang commented 8 years ago

for the subscriber, possible to have configurations look like below?

nchan_subscriber redis-pubsub;

or

nchan_subscriber_redis_pubsub_key "channel_$1";

slact commented 8 years ago

for the subscriber, possible to have configurations look like below? [...]

Unfortunately not. The publisher needs to be configured with a location block, so that's why nchan_upstream_message_source /publish/$1; refers to the internal publisher location block.

nchan_publisher_redis_pubsub_key "channel_$1";
This line makes me think nchan_channelid "$1" maps to redis pubsub channel "channel$1".

That's correct.

Anyway, I'll think about how to implement this. I've had other requests for a similar feature subscribing to upstream Nchan servers, and they will need similar methods of configuration and some similar code work. I'll update this when I come up with a satisfactory solution.

hellochenwang commented 8 years ago

If Nchan has an API like hiredis, then subscribing to upstream Nchan servers would be easier.

Thanks for the awesome Nchan!

SunChero commented 7 years ago

this could be freaking awesome !!

slact commented 6 years ago

resurrecting this to ask: is there any interest in sponsoring this feature?

santiagoalmeidabolannos commented 3 years ago

@slact can you post a newer version of the same example that you posted before? looks like some of the config values don't apply anymore