Reputation: 1429
I'm not sure I understand the difference really between using redis pub/ sub and using node/ express' event emitter. Does the pub/sub interact with my database in any way? In what cases would I use one over the other?
Upvotes: 4
Views: 4833
Reputation: 51440
Redis allows you to build fast and powerful inter-process communication. Redis pub/sub uses your Redis process to deliver your messages, but it never affects the data stored in Redis DB.
Event emitter works only within the single node process. So, even if you have a single node cluster, events, emitted by one worker, wont be visible to another.
Hence, event emitter is the perfect choice for within-the-process communication. But if you need inter-process communication, you should use Redis.
Upvotes: 15
Reputation: 8101
If Redis is your DB, then yes pub/sub interacts with the DB (but not the stored data) because it is Redis that is handling the execution of publishing to subscribed channels. And I wouldn't necessarily say that you would use one over the other. An event emitter will be used by the redis client to signal to your node application that Redis has published a message.
To really answer your question it depends on what you're using them for. If you only have one node instance running on one server, then perhaps an event emitter could serve as your message passing transport.
However, if you have multiple node instances, or multiple instances running on multiple servers, you could extend the event emitter to publish its messages to Redis thereby allowing the other node instances running on other servers to respond to the event.
Another good use case is socket.io. If you have say a chat room using sockets to broadcast between rooms, you would need to leverage Redis pub/sub if your chat application were spread across multiple servers.
Upvotes: 4