C.W.K.
Stream
Lesson 05 of 07 · published

Auto-Reconnection & Acknowledgment

~11 min · library, reconnect, ack

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

재연결 built-in

Socket.IO 가 exponential backoff + jitter 재연결 박스 채로 동봉. Track 2 에서 직접 짠 거 — 여기선 옵션 5개 설정하면 그냥 작동. 대부분 앱에 합리적 default: max attempt = Infinity, base delay 1s, cap 5-30s, jitter 0.5.

재연결 event

socket.io.on('reconnect') 로 성공 재연결 들음, reconnect_attempt 로 retry, reconnect_failed 로 영구 실패. UI state 'Connecting...' overlay, 'Offline' badge driving.

확인 용 acknowledgement

서버 event handler 가 값 (또는 list, multi-arg 위해) 리턴하면 클라쪽 ack payload 됨. Track 5 의 correlation-ID dance 가 코드 한 줄로 압축.

Code

재연결 option·javascript
const socket = io('https://api.example.com', {
  auth: { token },
  reconnection: true,
  reconnectionAttempts: Infinity,
  reconnectionDelay: 1_000,        // start delay
  reconnectionDelayMax: 30_000,    // cap
  randomizationFactor: 0.5,        // ±50% jitter
});

socket.io.on('reconnect_attempt', (n) => {
  console.log('reconnect attempt', n);
  setBanner('Reconnecting...');
});

socket.io.on('reconnect', (n) => {
  console.log('reconnected after', n, 'attempts');
  setBanner(null);
});

socket.io.on('reconnect_failed', () => {
  console.log('reconnect failed permanently');
  setBanner('Offline. Refresh to retry.');
});
서버가 ack data 리턴·python
@sio.on('chat:message')
async def chat(sid, data):
    saved = await db.save_message(data)
    return {'status': 'ok', 'id': saved.id, 'ts': saved.created_at}

External links

Exercise

재연결 option + 세 reconnect_* event 를 'Status: Online / Reconnecting / Offline' badge 에 wire. 30초 서버 죽이고 badge 가 state 사이 cycle 하는 거, 재시작 후 Online 으로 돌아오는 거 확인.

Progress

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

댓글 0

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

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