Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Chat (Go) — Dashboard Demo

Can token come from headers?

When Headers?
WebSocket connect Yes — Authorization: Bearer <jwt> on the Postman WebSocket request
Each chat message No — WebSocket frames are JSON only, not HTTP. Use body or rely on identity from connect header

After connect with a header token, you can send join_room / message without repeating the token.

Alternative (no JWT): send userId + username in the JSON body.


Run

go mod tidy
go run .

Workflow

  1. Connect → receive connect event with empty rooms: []
  2. Join room (required) → send join_room event
  3. Send messages → other members in the room broadcast

Auth options (pick one)

A) Header on connect (recommended for Postman)

  1. POST /api/auth/token → get JWT
  2. WebSocket URL: ws://127.0.0.1:8080/ws
  3. Header: Authorization: Bearer <jwt>
  4. Connect → receive connect with rooms: []
  5. Join room:
{
  "type": "join_room",
  "payload": { "roomId": "general" }
}
  1. Then send messages:
{
  "type": "message",
  "payload": {
    "roomId": "general",
    "text": "Hello!"
  }
}

B) userId + username in body (no JWT)

Connect, then join and message with identity in payload:

{
  "type": "join_room",
  "userId": "user-alice",
  "username": "alice",
  "payload": { "roomId": "general" }
}
{
  "type": "message",
  "userId": "user-alice",
  "username": "alice",
  "payload": {
    "roomId": "general",
    "text": "Hello!"
  }
}

C) Token in message body

{
  "type": "message",
  "token": "eyJhbG...",
  "payload": {
    "roomId": "general",
    "text": "Hello!"
  }
}

Priority: token → userId+username → identity from connect header.


Two-user demo

Tab Connect header Join Message
Alice Bearer <alice-jwt> join_room (no auth needed in payload) Message broadcasts to room
Bob Bearer <bob-jwt> same same

Both must explicitly join_room for general to receive each other's broadcasts.


Disconnect events

Someone closes Postman / loses connection → other clients in the room receive:

{
  "type": "disconnected",
  "user": { "id": "user-alice", "username": "alice" },
  "payload": {
    "reason": "connection_closed",
    "roomId": "general",
    "sessionId": "...",
    "memberCount": 1
  }
}

Then a leave event with updated memberCount.

Graceful disconnect (before closing WS):

{
  "type": "disconnect",
  "payload": { "reason": "user_logged_out" }
}

You get a disconnected ack; others in your rooms are notified too.

Event types

connect · join_room · leave · disconnect · disconnected · message · mute · unmute

Import postman/websocket-chat.postman_collection.json.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages