Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
Sequence Diagram
All Software Architecture templates

Real-Time Chat with WebSockets — Mermaid Sequence Diagram

Sequence diagram showing WebSocket connection lifecycle for a real-time chat application with presence tracking.

What this diagram shows

This Mermaid sequence diagram is one chat room with two browsers, a WebSocket server, and Redis. Alice connects to ws://chat/room/42. The server checks her token, subscribes to room:42, and returns the member list. It publishes a presence join, which Redis delivers to Bob. Alice's Hello is published on room:42 and broadcast to both sockets, including a delivery tick back to Alice. Bob replies on the same path. When Alice disconnects, the server publishes presence leave and unsubscribes.

Use it when you are specifying a room-based chat and need to show that the server does not hold the fan-out itself. Redis is the participant that makes presence and messages reach a second socket. It is also the diagram to use when someone proposes polling: the connect, subscribe, and disconnect messages are the lifecycle a poll loop does not have.

Alice and Bob are both browsers. The server is one process in the drawing. If you run multiple WebSocket nodes, the Redis participant is the reason either node can deliver Bob's reply.

Mermaid syntax in this template

participant aliases keep the lifelines short. Every socket message uses ->> for client-to-server and -->> for server-to-client pushes.

  • participant Alice as Alice (Browser)

    The alias includes the role. Repeat that pattern for Bob so both lifelines read as clients, not as people sitting outside the system.

  • Alice->>WS: Connect (ws://chat/room/42)

    The URL in the message text is the room key. Change the path when you model direct messages instead of rooms.

  • Redis-->>Bob: Presence update

    The dashed arrow from Redis to Bob is a push. Bob did not request it. That is the presence model.

  • WS->>Redis: Unsubscribe room:42

    The last message is cleanup. Leave it in. A chat diagram that ends on the last text message hides the leak of a subscribed channel.

Variations and ways to customize it

Add a typing indicator

A typing event should not be stored. Publish it on a side channel and do not draw a delivery tick, or Bob's client will treat typing as a message.

Alice->>WS: typing:start
WS->>Redis: Publish room:42 typing
Redis-->>Bob: Alice is typing

Reject a bad token on connect

The template authenticates inside the server and continues. Add an alt so a rejected token closes the socket before Subscribe.

alt Token valid
    WS->>Redis: Subscribe room:42
    WS-->>Alice: Connected + member list
else Token invalid
    WS-->>Alice: Close 4401
end

Model a second server node

Add participant WS2 and have Redis deliver Bob's session to WS2 instead of back to the same WS. Alice still talks only to WS. That is the multi-node case the single server drawing hides.

Diagram type

Sequence Diagram

Tags

WebSocketreal-timechatpub/subRedis
Open in Editor

Frequently asked questions

Why is Redis in a WebSocket chat sequence diagram?

The template uses Redis pub/sub so a message accepted on one connection is broadcast to every subscriber of room:42, including presence join and leave. Without that participant, Bob only receives messages if he shares a process with Alice.

How do I show message history on connect?

Add a database participant and a message from WS to DB before Connected + member list. Return the last N messages in that same response. Live publishes stay on Redis; history is the one request/response.

What does the delivery tick mean in the diagram?

WS-->>Alice: Message delivered is an acknowledgement from the server after the publish, not a read receipt from Bob. Add a separate Read message from Bob if you need receipts.

Can I change the room URL without breaking Mermaid?

Yes. The ws:// URL is message text, not syntax. Keep the participant IDs (Alice, WS, Redis, Bob) stable so the arrows still parse.

Related templates and guides

All Software Architecture templates