Database2024년 12월 12일2분 읽기

Redis Pub/Sub으로 실시간 알림 시스템 구축

YS
YoungSam
조회 1231

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를 사용하세요.

RedisPub/SubRealtime

댓글 0

아직 댓글이 없습니다.