Real-time measurement streaming
WebSocket-based streaming facilitates continuous data flow between your application and Hume’s models, providing immediate feedback and insights.
Key features
- Real-time data processing: Leveraging WebSockets, this API allows for the streaming of data to Hume’s models, enabling instant analysis and response. This feature is particularly beneficial for applications requiring immediate processing, such as live interaction systems or real-time monitoring tools.
- Persistent, two-way communication: Unlike traditional request-response models, the WebSocket-based streaming maintains an open connection for two-way communication between the client and server. This facilitates an ongoing exchange of data, allowing for a more interactive and responsive user experience.
- High throughput and low latency: The API is optimized for high performance, supporting high-volume data streaming with minimal delay. This ensures that applications can handle large streams of data efficiently, without sacrificing speed or responsiveness.
Applications and use cases
WebSockets are ideal for a wide range of applications that benefit from real-time data analysis and interaction. Examples include:
- Live customer service tools: enhance customer support with real-time sentiment analysis and automated, emotionally intelligent responses
- Interactive educational platforms: provide immediate feedback and adaptive learning experiences based on real-time student input
- Health and wellness apps: support live mental health and wellness monitoring, offering instant therapeutic feedback or alerts based on the user’s vocal or textual expressions
- Entertainment and gaming: create more immersive and interactive experiences by responding to user inputs and emotions in real time
Getting started with WebSocket streaming
Integrating WebSocket-based streaming into your application involves establishing a WebSocket connection with Hume AI’s servers and streaming data directly to the models for processing.
Streaming is built for analysis of audio, video, and text streams. By connecting to WebSocket endpoints you can get near real-time feedback on the expressive and emotional content of your data.
Install the Hume Python SDK
First, ensure you have installed the SDK using pip
or another package manager.
Emotional language from text
This example uses our Emotional Language model to perform sentiment analysis on a children’s nursery rhyme.
If you haven’t already, grab your API key.
Your result should look something like this:
Facial expressions from an image
This example uses our Facial Expression model to get expression measurements from an image.
Speech prosody from an audio or video file
This example uses our Speech Prosody model to get expression measurements from an audio or video file.
Streaming with your own WebSockets client
To call the API from your own WebSockets client you’ll need the API endpoint, a JSON message, and an API key header/param. More information can be found in the Expression Measurement API reference.
To get started, you can use a WebSocket client of your choice to connect to the models endpoint:
Make sure you configure the socket connection headers with your personal API key
The default WebSockets implementation in your browser may not have support for headers. If that’s the case you can set the apiKey query parameter.
And finally, send the following JSON message on the socket:
You should receive a JSON response that looks something like this:
Sending images or audio
The WebSocket endpoints of the Expression Measurement API require that you encode your media using base64. Here’s a quick example of base64 encoding data in Python:
API limits
- WebSocket duration limit: connections are subject to a default timeout after one (1) minute of inactivity to ensure unused connections are released.
- WebSocket message payload size limit: the size limit for a given payload depends on the type of content being transmitted and its dimensions.
- Video: 5000 milliseconds (5 seconds)
- Audio: 5000 milliseconds (5 seconds)
- Image: 3,000 x 3,000 pixels
- Text: 10,000 characters
- Request rate limit: HTTP requests (e.g. WebSocket handshake endpoint) are limited to fifty (50) requests per second.
FAQ
What are WebSockets?
WebSockets are a communication protocol that enables real-time, two-way communication between a client and a server over a single, long-lived connection. They provide a persistent connection that allows both the client and the server to initiate communication at any time.
Handling reconnects
Streaming will disconnect every minute to ensure unused connections are released. You will need to reconnect by building reconnect logic into your application. Implementation of reconnect logic will depend on the language and framework of your client application.
Please see our TypeScript streaming sandbox example for a sample implementation.
Handling connection failures
WebSocket connections can experience disruptions due to network issues or other factors. Implement error handling mechanisms to gracefully handle connection failures. This includes handling connection timeouts, connection drops, and intermittent connection issues. Implement reconnection logic to automatically attempt to reconnect and resume communication when a connection is lost.
Implementing error handling
Hume WebSockets endpoints can return errors in response to invalid requests, authentication failures, or other issues. Implement proper error handling to interpret and handle these errors in your application. Provide meaningful error messages to users and handle any exceptional scenarios gracefully. To prevent unknowingly initiating too many errors we have put a limit on how many of the same errors you can have in a row. For a full list of the error responses you can expect, please see our API errors page.
Keeping WebSockets open
The benefits of using a the WebSocket is the persistent connection. The open socket should be kept open until the application is done utilizing the service and then closed. Avoid opening a new connection for each file or payload you send to the API. To ensure that context does not leak across multiple unrelated files you can use the reset_stream parameter.