DEV Community

Cover image for Simple Pub/Sub with Redis
Sibelius Seraphini for Woovi

Posted on

Simple Pub/Sub with Redis

Woovi is building an instant payment infrastructure for Pix (Brazil's most popular payment method).
When using Pix a payment is settled in at most 10 seconds, and the users expect fast feedback when making a payment.

To be able to provide fast feedback on the front end we need to use WebSockets. The WebSocket server needs to track what events each WebSocket client is waiting for.
Publisher-Subscriber is a design pattern that lets us define what events we want to subscribe to and be able to publish event messages to them.
In a production environment where you run multiple pod instances of your service, you can keep track of publish and subscribes in memory, you need a system to communicate over the server instances. Redis provides this Pub/Sub feature.

In this article, we will provide a simple Pub/Sub implementation using Redis

Redis Pub/Sub

const redis = new Redis(config.REDIS_HOST);

redis.publish('payment-received', JSON.stringify(payload);

redis.subscribe('payment-received', (payload) => {
   // notify WebSocket client
});

Enter fullscreen mode Exit fullscreen mode

In Conclusion

In a few lines of code, you can have a powerful Pub/Sub system for your system. All the complexity is managed by Redis.
Be careful when designing your subscription topics to make it easy to understand and maintain.
You can read the other use cases of Redis here Redis at Woovi


Woovi is an innovative startup revolutionizing the payment landscape. With Woovi, shoppers can enjoy the freedom to pay however they prefer. Our cutting-edge platform provides instant payment solutions, empowering merchants to accept orders and enhance their customer experience seamlessly.

If you're interested in joining our team, we're hiring! Check out our job openings at Woovi Careers.

Top comments (2)

Collapse
 
jonathangaldino profile image
Jonathan

What do you guys do to figure out which WebSocket client to send the message to?

Collapse
 
sibelius profile image
Sibelius Seraphini

we have a mapper for this