Redis Pub/Sub이란
Redis의 발행/구독 메시징 패턴입니다. 메시지 브로커 없이 간단한 실시간 통신을 구현할 수 있습니다.
Node.js 구현
import Redis from "ioredis";
// Publisher
const pub = new Redis();
await pub.publish("notifications", JSON.stringify({
userId: "user1",
message: "새 댓글이 달렸습니다",
}));
// Subscriber
const sub = new Redis();
sub.subscribe("notifications");
sub.on("message", (channel, message) => {
const data = JSON.parse(message);
sendSSE(data.userId, data.message);
});주의: Pub/Sub은 메시지 영속성을 보장하지 않습니다. 중요한 메시지는 Redis Streams를 사용하세요.
댓글 0