DEV Community

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

Posted on

6 1 1 1 1

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay