Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidystephenson/d2502ab827b3f1590810a216d408a77d to your computer and use it in GitHub Desktop.

Select an option

Save davidystephenson/d2502ab827b3f1590810a216d408a77d to your computer and use it in GitHub Desktop.
# Socket.io Chat App
## 1. Connection & Identity Registration
- Client connects with a username
- Server accepts or rejects the connection
- Server assigns a socket ID
- User is removed from state on disconnect
### Topics
- `io.on("connection")`
- `socket.id`
- `socket.handshake.auth`
- `io.use(...)`
- `disconnect` / `disconnecting`
- Disconnect reason
## 2. Namespaces
- `/chat` — normal chat behavior
- `/admin` — read server-originated monitoring events only
### Topics
- `io.of("/namespace")`
- Namespace-level `connection`
- Namespace-specific middleware
- Namespace isolation
- Server-only emits
## 3. Room Membership
- Clients can join and leave named rooms
- Server tracks room membership
- Messages to rooms are allowed only for members
### Topics
- `socket.join(room)`
- `socket.leave(room)`
- `socket.rooms`
- Default socket ID room
## 4. Direct Messaging
Client sends a direct message to another client
### Topics
- Implicit socket ID rooms
- `io.to(socketId).emit(...)`
## 5. Room Messaging (Group Chat)
Client sends a message to a room to all members of that room
### Topics
- `io.to(room).emit(...)`
- `io.in(room).emit(...)`
- Room-scoped broadcasting
## 6. Global Messaging
Client sends a message to all connected clients
### Topics
- `io.emit(...)`
- Namespace-wide broadcast
## 7. Broadcast-Except-Sender Messaging
Client sends a message to everyone except themselves
Server excludes sender from delivery
### Topics
- `socket.broadcast.emit(...)`
## 8. Acknowledged Messaging
- Client sends a message
- Server validates message payload
- Server responds with:
- success acknowledgment
- error acknowledgment
### Topics
- Event acknowledgements
- Callback-based responses
- `socket.timeout(ms)`
## 9. Volatile Events (Typing Indicators)
Clients emit typing indicators, delivery is not guaranteed
### Topics
- `socket.volatile.emit(...)`
## 10. Presence Tracking
Server tracks total connected users and users per room Presence updates are emitted on:
- connect
- room join
- room leave
- disconnect
### Topics
- Connection lifecycle hooks
- `disconnecting` timing
- Server-originated emits
## 11. Reconnection & State Recovery
Client reconnects automatically and restores user identity and room membership
### Topics
- `socket.recovered`
- `connectionStateRecovery`
- Reconnection handling
## 12. Error Emission
Invalid actions emit structured error events with code and context
### Topics
- Custom error events
- Error propagation patterns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment