All connections require a valid JWT token. Obtain a token by logging in via
POST /api/auth/login with username and password.
The token must be included in all requests:
Append ?token=<jwt> to the WebSocket URL:
Include an Authorization: Bearer <jwt> header.
The built-in API client handles this automatically when a token is stored in sessionStorage.
The easiest way to integrate with Squawk is using the SquawkWebSocket library.
This standalone ES module handles WebSocket connections, audio playback, and stream management.
https://example.com/lib/listener_websocket.js
| Parameter | Type | Required | Description |
|---|---|---|---|
url |
string | Required | WebSocket endpoint URL |
audioContext |
AudioContext | Required | Browser AudioContext instance for audio playback |
| Method | Parameters | Description |
|---|---|---|
connect() |
none | Connect to the WebSocket server |
disconnect() |
none | Disconnect from the server |
subscribe() |
keywords: string[] | Subscribe to keywords |
unsubscribe() |
keywords: string[] | Unsubscribe from keywords |
ping() |
none | Send a keepalive ping |
get_status() |
none | Get current connection status |
unlock_audio() |
none | Unlock audio after user gesture (call in response to click/tap) |
is_audio_locked() |
none | Check if audio is locked due to autoplay policy |
on() |
event: string, handler: function |
Listen for events |
| Event | Data | Description |
|---|---|---|
connected |
none | WebSocket connected |
disconnected |
CloseEvent | WebSocket disconnected. Check event.code — 4003 means auth failed |
stream_start |
StreamStartMessage | Audio stream started (receives StreamStartMessage) |
stream_end |
{ session_id: string } | Audio stream ended |
subscribed |
{ keywords: string[] } | Subscription confirmed |
clip_received |
ClipMessage | Historical clip received |
audio_locked |
none | Autoplay blocked - user interaction needed. Call unlock_audio() after user gesture. |
audio_unlocked |
none | Audio successfully unlocked after user interaction |
error |
Error | Error occurred |
status_change |
ConnectionStatus | Connection status changed |
/api/ws
Connect to this endpoint to establish a WebSocket connection for receiving audio streams. The server accepts text commands (JSON) and sends both text messages (JSON metadata) and binary messages (audio data).
Subscribe to one or more keywords to receive matching audio streams.
Remove subscriptions for specific keywords.
Keep the connection alive. The server will respond with a pong.
Sent when a broadcaster starts streaming audio matching your subscribed keywords.
Sent when the broadcaster stops streaming.
Sent when a historical clip is available. Followed by binary audio data.
Confirmation that your subscription was registered.
Sent when an error occurs.
After a stream_start message, the server sends binary audio chunks as
ArrayBuffer messages. The SquawkWebSocket library automatically handles
these and feeds them to the audio element.
Similarly, after a clip message, the next binary message contains
the complete clip audio data.
This demo uses the SquawkWebSocket library to connect to
/api/ws and subscribes to
asset:equities.
When a broadcaster transmits with this keyword, audio will play automatically.