The easiest way to integrate with Squawk broadcasting is using the BroadcastStream class.
This standalone ES module handles microphone access, audio recording, and HTTP streaming to the Squawk ingest endpoint.
dist/lib/broadcast_standalone.js
BroadcastStream is the recommended high-level API for broadcasting.
It manages the microphone, streams audio to /api/ingest, and provides connection status updates.
<script type="module">
import { BroadcastStream } from './dist/lib/broadcast_standalone.js';
const stream = new BroadcastStream('/api/ingest', {
on_status_change: (status) => {
console.log('Status:', status); // 'disconnected' | 'connected' | 'streaming' | 'error'
document.getElementById('status').textContent = status;
},
on_listener_count: (count) => {
console.log('Stream reached', count, 'listeners');
},
on_error: (message) => {
console.error('Error:', message);
},
on_recording_complete: (stats) => {
console.log(`Streamed ${stats.bytes_streamed} bytes to ${stats.dispatched_to} listeners`);
},
on_level_change: (level) => {
// Update VU meter (0.0 to 1.0)
document.getElementById('level').style.width = `${level * 100}%`;
},
});
// Start streaming to specific channels
const channels = ['equities', 'bonds'];
await stream.start_recording(channels);
// Stop streaming
stream.stop();
</script>
High-level class that manages the complete broadcast workflow. Handles microphone access, HTTP streaming to the ingest endpoint, and connection state management.
new BroadcastStream(url: string, callbacks: BroadcastStreamCallbacks)
url
The ingest endpoint URL (e.g., '/api/ingest')
start_recording(channels: string[])
Start recording and streaming. Channels are prefixed with 'asset:' and sent as keywords
stop()
Stop recording and close the stream
streaming: boolean
Whether currently streaming
on_status_change(status: ConnectionStatus)
Called when connection status changes: 'disconnected' | 'connected' | 'streaming' | 'error'
on_listener_count(count: number)
Called when stream ends with the number of listeners who received the audio
on_error(message: string)
Called when an error occurs
on_recording_complete?(stats: object)
Optional. Called when recording completes with bytes_streamed and dispatched_to stats
on_level_change?(level: number)
Optional. Called with audio level 0.0-1.0 for VU meter updates
High-level recorder that handles microphone access and audio recording. Manages the MediaStream lifecycle and provides a simple start/stop interface.
new MicrophoneRecorder(callbacks: RecorderCallbacks)
start(timeslice?: number)
Start recording. Timeslice is chunk duration in ms (default: 100)
stop()
Stop recording and release microphone
is_recording: boolean
Whether currently recording
mime_type: string | null
The MIME type being used for recording (e.g., "audio/webm;codecs=opus")
Lower-level recorder that works with an existing MediaStream. Use this if you need more control over the audio source.
new AudioRecorder(stream: MediaStream, callbacks: RecorderCallbacks)
start(timeslice?: number)
Start recording from the provided stream
stop()
Stop recording
All callbacks are optional but at minimum you should handle on_chunk.
on_chunk(data: ArrayBuffer)
Called for each audio chunk (based on timeslice)
on_stop()
Called when recording stops
on_error(message: string)
Called on errors (e.g., microphone denied)
on_level_change(level: number)
Called with audio level 0.0-1.0 for VU meter
/api/ingest/stream
Connect to this endpoint to stream audio to the Squawk server as a broadcaster. The server expects a JSON authentication message followed by binary audio chunks.
/api/ingest/streamMust be sent immediately after connection. Replace with your actual broadcaster credentials.
{
"type": "auth",
"broadcaster_id": "your_broadcaster_id",
"api_key": "your_api_key"
}
Signals the start of a broadcast. Keywords determine which listeners receive the audio.
{
"type": "stream_start",
"keywords": ["asset:equities", "region:us"],
"content_type": "audio/webm;codecs=opus"
}
Signals the end of the broadcast.
{
"type": "stream_end"
}
Keep the connection alive.
{
"type": "ping"
}
After sending the stream_start message, send audio chunks as binary
ArrayBuffer messages. The server accepts WebM/Opus format.
Use the MicrophoneRecorder class to handle recording and chunking.
Authentication successful.
{
"type": "authenticated",
"broadcaster_id": "your_broadcaster_id"
}
Server acknowledges stream start.
{
"type": "stream_started",
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"keywords": ["asset:equities"]
}
Server acknowledges stream end.
{
"type": "stream_ended",
"session_id": "550e8400-e29b-41d4-a716-446655440000"
}
Sent when an error occurs.
{
"type": "error",
"message": "Invalid authentication"
}
This demo uses the BroadcastStream class to record and stream to /api/ingest.
Select channels below, then start recording to broadcast.