본문 바로가기
C.W.K.
Stream
Lesson 04 of 08 · published

알림 시스템

~10 min · app, notifications

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

polling 하지 말고 밀어 줘

알림은 서버가 밀어 주기에 가장 잘 맞는 사례야. 사용자가 요청하지 않았지만 서버에는 전할 소식이 있지. 자료형이 있는 메시지로 보내고 클라이언트 UI 가 토스트, 배지, 소리 중 알맞은 표현을 골라.

여러 기기로 펼치기

같은 사용자가 휴대폰, 노트북, 태블릿을 열어 두었다면 셋 모두에 보여야 해. 더 나아가 어느 기기에서든 확인하면 모두에서 지울 수도 있지. 트랙 4 의 여러 기기 지원 관리자를 쓰면 전송 자체는 한 줄이면 돼.

알림 흐름에는 SSE 로 충분해

알림은 서버에서 클라이언트로만 흘러가므로 단순함에서는 SSE 가 이겨. 같은 연결에서 다른 양방향 흐름까지 처리해야 할 때 WebSocket 을 골라.

Code

서버: 사용자에게 알림 밀어 주기·python
import uuid
from datetime import datetime

async def notify(manager, user_id: str, *, title: str, body: str,
                  level: str = 'info', url: str | None = None):
    msg = {
        'type': 'notification',
        'data': {
            'id':    str(uuid.uuid4()),
            'title': title,
            'body':  body,
            'level': level,
            'url':   url,
            'ts':    datetime.utcnow().isoformat() + 'Z',
        },
    }
    sent = await manager.send_to_user(user_id, msg)
    if sent == 0:
        # User offline; persist for delivery on next connect.
        await db.notifications.insert(user_id, msg)
클라이언트: 토스트와 배지·javascript
ws.on('notification', (data) => {
  showToast({
    title:   data.title,
    body:    data.body,
    level:   data.level,           // info / warning / success / error
    onClick: () => data.url && (location.href = data.url),
  });
  unread += 1;
  setBadge(unread);
});

External links

Exercise

notify() 서버 함수와 클라이언트 토스트를 만들고 REST 엔드포인트 POST /admin/notify 에서 실행해. 같은 사용자의 브라우저 탭 세 개와 다른 사용자 탭 하나를 열어, 대상 사용자만 세 탭 모두에서 토스트를 보는지 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.