Progettare un backend di chat in tempo reale con gRPC
Esamini un caso reale: la progettazione di un backend di chat scalabile e in tempo reale con streaming bidirezionale gRPC, fan-out e presenza.
Progettare un backend di chat in tempo reale con gRPC è una lezione gRPC & High Performance APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento gRPC & High Performance APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso gRPC & High Performance APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Case Study
We design a chat backend that must deliver messages instantly to thousands of connected users. gRPC bidirectional streaming is a natural fit for this long-lived, two-way flow.
Defining the Service
The core is a bidi stream: clients send messages and receive others' messages over the same call.
service Chat {
rpc Connect(stream ClientEvent) returns (stream ServerEvent);
}Per-Connection Sessions
Each open stream is a user session. The server tracks the stream handle so it can push messages to that specific user later.
The Fan-Out Problem
When one user posts to a room, the server must deliver it to every other member's stream. This fan-out is the heart of chat scaling.
In-Process Hub
A simple design keeps a hub mapping rooms to subscriber channels. Posting iterates members and sends to each.
func (h *Hub) Broadcast(room string, msg *ServerEvent) {
for _, sub := range h.rooms[room] {
sub.outbox <- msg
}
}Scaling Past One Instance
Users on different server instances must still see each other. Introduce a message bus (Redis Pub/Sub, NATS, Kafka) so instances relay messages to one another.
Cross-Instance Flow
An instance publishes new messages to the bus; every instance subscribes and forwards to its own local connections. This decouples delivery from where users connect.
Presence and Typing
Presence (online/offline) and typing indicators are just more event types on the stream. Track last-seen timestamps and broadcast presence changes via the same fan-out path.
Backpressure per Client
A slow client must not stall the hub. Give each session a bounded outbox; if it overflows, drop or disconnect that client instead of blocking everyone.
select {
case sub.outbox <- msg:
default:
disconnect(sub) // slow consumer
}Reconnection and Delivery
Networks drop. Clients reconnect and resume from a last-seen message id. Persist recent history so missed messages can be replayed on reconnect.
Operational Concerns
Production chat needs:
- Keepalive to detect dead connections
- Authentication on connect (token in metadata)
- Metrics on active streams and fan-out latency
- Graceful drain on deploy
Quick Check
Test your chat-design knowledge.
Recap
You designed a real-time chat backend:
- Bidirectional streaming models each user session
- A hub fans out room messages to subscribers
- A message bus relays across multiple instances
- Presence/typing are extra event types; per-client backpressure protects the hub
- Reconnection replays missed messages; ops needs keepalive, auth, metrics, drain
Domande Frequenti
La lezione «Progettare un backend di chat in tempo reale con gRPC» è gratuita?
Sì — il testo completo di «Progettare un backend di chat in tempo reale con gRPC» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso gRPC & High Performance APIs, passa a CoddyKit PRO. Il corso gRPC & High Performance APIs include 4 lezioni in totale.
Cosa imparerò in «Progettare un backend di chat in tempo reale con gRPC»?
Esamini un caso reale: la progettazione di un backend di chat scalabile e in tempo reale con streaming bidirezionale gRPC, fan-out e presenza. Eserciti gRPC & High Performance APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare gRPC & High Performance APIs?
Non è richiesta alcuna esperienza precedente. gRPC & High Performance APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Progettare un backend di chat in tempo reale con gRPC»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione gRPC & High Performance APIs?
Sì. Ogni lezione gRPC & High Performance APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Creazione di gateway ad alto throughput
- Pattern avanzati di resilienza
- Il futuro delle API ad alte prestazioni
- Progettare un backend di chat in tempo reale con gRPC