# Chat over WebSocket: frontend integration guide Chat (conversations between a player and their coaches) now runs over one WebSocket. This guide lists what to change in the app. ## 0. Summary of what is new 1. **Chat socket.** One WebSocket, `wss:///v1/chat/ws`, carries every chat action (list, open, send, mark read) and pushes new messages live. The app no longer polls. See §2 to §6. 2. **Photos and videos in chat.** Two new message kinds, `image` and `video`, can be sent and received, over the socket and in every thread read. See §4.1. 3. **The file URL is in `media.url`.** Each `image` and `video` message has a key named **`media`**, and the link to show or play is the **`url`** field inside it: | Key | Meaning | |---|---| | `media.url` | The link to the photo or video. Display or play this. | | `media.upload_id` | Permanent id of the file. Use it as the cache key. | | `media.expires_at` | When `media.url` stops working (about one hour after it was issued) | | `media.seconds` | Video length in seconds; `null` for a photo | 4. **Links are temporary, files are not.** `media.url` is a signed link that expires; the server issues a fresh one every time a message is sent to the app. See "Why the link expires" in §4.1. The thread object and the existing message kinds (`text`, `voice`, `session`, `review`) are unchanged: they are the ones in `FRONTEND_INTEGRATION.md` §14.4. ## 1. What changes for the app | Before (REST) | Now (socket command) | |---|---| | `GET /v1/threads` | `threads.list` | | `GET /v1/threads/{id}` | `thread.get` | | `GET /v1/sessions/{id}/thread` | `thread.for_session` | | `POST /v1/threads/{id}/messages` | `message.send` | | `POST /v1/threads/{id}/read` | `thread.read` | | `POST /v1/coach/players/{id}/thread` | `thread.ensure` | | Re-fetching threads to see new messages | Live events: `message.new`, `thread.read`, `thread.new` | - **Stop polling.** Remove every timer or focus refresh that re-fetches threads. Load once over the socket, then apply the events. - **Files still upload over HTTP.** For a voice note, photo or video, get the presigned URL from `POST /v1/uploads`, PUT the file, then send `message.send` with the `upload_id` (§4.1). Files never go through the socket. - **Older app builds will receive `image` and `video` messages** in threads once someone sends one. Make sure every build in use renders an unknown `kind` as a placeholder instead of crashing. - **Push notifications are unchanged.** A new message still creates an inbox notification and a push. While the socket is open and the app is in the foreground, ignore the chat push and show the live event instead. - **The REST endpoints still work** so released app builds keep working, and whatever they write reaches open sockets as live events. New code should not call them. ## 2. Connecting ``` wss:///v1/chat/ws ``` 1. Open the socket when the user is signed in and the app is in the foreground. One socket per app instance is enough; it serves every thread. 2. **The first frame must be `auth`,** within 10 seconds: ```json {"id":"1","type":"auth","token":""} ``` Reply: `{"id":"1","type":"auth.ok","data":{"account_id":"...","expires_at":"2026-11-17T18:30:00Z"}}`. 3. If the first frame is anything else, or the token is invalid, the server replies with an error and closes the socket with code **4401**. Refresh the token over REST (`POST /v1/auth/refresh`) and connect again. The token is sent in a frame, not in the URL, so it does not end up in server or proxy logs. ### When the access token expires The socket stays open, but it stops working until you authenticate again: - Commands get `{"type":"error","status":401,"error":{"code":"TOKEN_EXPIRED",...}}`. - **Live events are not delivered while the token is expired.** So refresh before `expires_at` (from `auth.ok`), then send `auth` again **on the same socket** with the new token. After any gap (expired token or reconnect), reload the open thread and the list (§6), because events from the gap are not replayed. ## 3. Frame format Every frame is a JSON text frame. **Command (app → server).** `id` is any string you choose; the reply repeats it. Parameters sit next to `type`. ```json {"id":"42","type":"thread.get","thread_id":"6f1c...","message_limit":50} ``` **Reply (server → app).** One per command. ```json {"id":"42","type":"thread.get.ok","data":{ ... }} {"id":"42","type":"error","status":404,"error":{"code":"NOT_FOUND","message":"...","fields":{...}}} ``` - `error` is the same object as the REST error envelope (`FRONTEND_INTEGRATION.md` §3), and `status` is the HTTP status the REST endpoint would have returned. Reuse your existing error handling. - Replies can arrive in a different order from the commands, and events can arrive in between. Match replies by `id`. **Event (server → app).** No `id`. ```json {"type":"message.new","data":{ ... }} ``` **Keep-alive.** `{"id":"7","type":"ping"}` → `{"id":"7","type":"pong"}`. Send one every 25 seconds or so if your network stack drops idle sockets. ## 4. Commands All ids are UUID strings. Unknown parameters are rejected with 400 `VALIDATION`. ### `threads.list` | Param | Type | Notes | |---|---|---| | `mode` | `"h"` \| `"p"` | optional | | `player_id` | uuid | optional; coaches filter by player | | `unread` | bool | optional | | `cursor` | string | optional; `next_cursor` from the previous page | | `limit` | int | 1–200, default 50 | `data`: `{"items":[Thread, ...],"next_cursor":"...|null"}`. Each thread carries its latest 20 messages. ### `thread.get` | Param | Type | Notes | |---|---|---| | `thread_id` | uuid | required | | `message_cursor` | string | optional; `next_message_cursor` of the thread, to load older messages | | `message_limit` | int | 1–100, default 50 | `data`: `{"thread": Thread}`. ### `thread.for_session` `session_id` (uuid, required). `data`: `{"thread": Thread}`. Error 404 `THREAD_NOT_FOUND` when the session has no conversation yet. ### `message.send` ```json {"id":"9","type":"message.send","thread_id":"6f1c...","idempotency_key":"", "message":{"kind":"text","text":"Thanks coach"}} ``` `message` is one of: ```json {"kind":"text","text":"1 to 500 characters"} {"kind":"voice","upload_id":"