Web Socket

Real-time Voice Chat API Documentation

WebSocket Endpoints

1. Media Stream Endpoint

URL: wss://voice-chat-socket.med2lab.com/media-stream Protocol: WebSocket Secure (WSS) Description: Handles real-time audio streaming between client and OpenAI's voice model.

Query Parameters

Parameter
Type
Required
Default
Description

token

string

Yes

-

Authorization token

topic_id

string

No

'102'

Topic identifier

Example Usage:

wss://voice-chat-socket.med2lab.com/media-stream?token=eyJhbGciOiJIUzI1NiIs...&topic_id=102

Connection Flow

  1. Client initiates WSS connection

  2. Connection established with WebSocket

  3. Bi-directional audio streaming begins

Audio Data Format

  • Client to Server:

    • Raw audio buffer

    • 16-bit PCM

    • Sample Rate: 16kHz

    • Channels: Mono

    • Chunk Size: 4096 samples

  • Server to Client:

    • Audio: Base64 encoded audio data

    • Text: UTF-8 encoded transcripts

2. Chat Stream Endpoint

URL: wss://voice-chat-socket.med2lab.com/chat-stream Protocol: WebSocket Secure (WSS) Description: Handles real-time chat interactions with voice capability.

Query Parameters

Parameter
Type
Required
Default
Description

token

string

Yes

-

Authorization token

CaseId

string

No

'653'

Case identifier

Example Usage:

Special Response Markers

The system includes special markers in responses:

  • [[EMAIL]]: Added when user mentions email

  • [[ENDCHAT*Feedback*Chat xong rồi !!!]]: Added when user mentions going to sleep

Client Integration Examples

ReactJS Example

Python Example

C# Example

Error Handling

The server may close the connection with the following error codes:

  • 1008: Authorization errors (missing/invalid token, missing API key)

  • Connection errors with OpenAI service

  • Internal server errors

Security Considerations

  1. All connections must use WSS (WebSocket Secure)

  2. Token-based authentication is required for all endpoints

  3. Server enforces proper error handling and connection timeouts

Last updated