Pernah nge-chat di WhatsApp, balasan langsung muncul tanpa refresh? Atau Google Docs yang kerjasama bareng — temen ngetik di laptop dia, langsung muncul di laptop kamu? Atau Tokopedia yang nampilin notifikasi "barang sudah dikirim" tepat saat kurir pickup? Itu **real-time**: data update saat event terjadi, gak nungguin user refresh. Tanpa real-time, web kerasa kuno (kayak Friendster era 2005, harus F5 terus buat liat update).
💡 Real-time != instant. Real-time berarti delay sangat kecil (millisecond), bukan literal nol. Trader saham minta < 10ms. Chat OK 100-500ms. Live dashboard OK 1-2 detik. Tentukan latency requirement berdasarkan use case sebelum pilih teknologi.
Real-time di Web
Real-time berarti data ter-update di client saat terjadi perubahan di server, tanpa user harus refresh. Chat, notifications, collaborative editing, live dashboard — semua butuh real-time.
Request-Response vs Real-time
// Traditional (request-response)
Client: "Ada pesan baru?" → Server: "Tidak."
Client: "Ada pesan baru?" → Server: "Tidak."
Client: "Ada pesan baru?" → Server: "Ya! Ini pesannya."
// Polling: client terus bertanya
// Real-time (push)
Client: "Kasih tahu saya kalau ada pesan baru" (subscribe)
... (silence) ...
Server: "Pesan baru dari Budi: Halo!" (push)
Server: "Pesan baru dari Rina: OK!" (push)
// Server push saat event terjadi
Use Cases
- Chat — WhatsApp, Slack, Discord
- Notifications — Push notifications, toast messages
- Collaborative editing — Google Docs, Figma
- Live dashboard — Analytics, monitoring, trading
- Gaming — Multiplayer games, turn-based games
- Live updates — Sports scores, election results, delivery tracking
Technologies
- WebSocket — Full-duplex, bidirectional. Most flexible.
- Server-Sent Events (SSE) — Server → client only. Simpler.
- Long Polling — HTTP-based fallback. Works everywhere.
- WebRTC — Peer-to-peer. Video/audio calls, screen sharing.
🎭 Analogi sehari-hari
Polling = nelpon orang setiap menit nanya "udah ada update?". Capek + boros pulsa. Real-time (WebSocket) = saluran telepon yang tetap nyala. Kalau ada update, langsung kasih tau. Kalau gak ada, diam aja. Lebih efisien, latency rendah. Real-time = telepon, polling = SMS continuous.
⚠️ Jebakan yang sering ditemui
- WebSocket sticky to server instance — server mati = koneksi putus. Wajib reconnect logic + Redis Pub/Sub buat cross-server
- Pakai WebSocket untuk semua — kalau cuma server → client (notifikasi, live feed), SSE lebih simpel + auto-reconnect built-in
- Lupa scaling — 10rb concurrent connection ≠ 1rb x 10. Connection pooling, sticky session, load balancer L7
- No heartbeat = ghost connection (client mati tapi server gak tau). Implement ping/pong
- Send raw JSON tanpa kompresi = bandwidth hog. Pakai binary protocol (MessagePack) atau compression
TL;DR: Real-time = data update saat event terjadi, no refresh. Use cases: chat, notifications, live dashboard, collaborative edit, gaming. 4 teknologi: WebSocket (bidirectional, paling fleksibel), SSE (server→client, simple), Long Polling (HTTP fallback), WebRTC (P2P video/audio). Pilih sesuai latency + bidirectional needs. Wajib di app modern.