Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why can't I have a single Redis client acting as PUB and Sub in the same connection?

Why can't I have a single Redis client acting as PUB and Sub in the same connection?

Problem

My mental model was that of a 'chat', where I am subscribed to a certain channel and I can publish messages to this channel.

Problem courtesy of: Draconar

Solution

Since pub/sub is asynchronous, the published message could appear at any time, including when you are expecting the response to a command.

Although Redis is single threaded, which normally prevents that sort of thing, network latency can cause some interesting effects - depending on the content of the messages, you could receive a valid response to a command before the server has actually received it.

That said, you could probably use a single Connection if you really wanted to - "should not" is not the same as "can not", and redis follows a simple design philosophy of not trying to prevent you from shooting yourself in the foot. However, it is a lot easier to just open two connections to the server. If you hit connection limits with two connections per client you will probably run into problems with one connection per client fairly soon anyway.

Solution courtesy of: Tom Clarkson

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Why can't I have a single Redis client acting as PUB and Sub in the same connection?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×