The Lens Events API enables real-time control and data streaming for active lens sessions via WebSocket connections. This allows you to interact with Newton models, configure input streams, and receive real-time inference results.Documentation Index
Fetch the complete documentation index at: https://docs.archetypeai.app/llms.txt
Use this file to discover all available pages before exploring further.
Connection Setup
1. Create a Lens Session
First, create a lens session using the REST API:session_endpoint for WebSocket connection (see Create Lens Session for the full response shape):
2. Connect to WebSocket
For JavaScript, use the
Sec-WebSocket-Protocol header to pass authentication and protocol version.Event Message Format
All event messages are JSON objects with a requiredtype field and optional event_data field:
session.status (see below), every server response carries a type field suffixed with .response and wraps the result in event_data:
event_data would be empty, it is returned as null.
Session Events
session.status
Query the current status of the active session.session.status returns the full session metadata record (the same shape as Create Lens Session), not the {type, event_data} envelope.
session.validate
Run validation checks on the current session, including health checks and model connectivity.session.read
Read pending event messages from the platform, such as log messages or asynchronous inference results.Each client should generate a unique
client_id to enable parallel reading. Use a new client_id to reset message stream playback.session.destroy
Destroy the active session directly via the control stream.Input Stream Configuration
input_stream.set
Configure input data streams to automatically feed data through the lens.RTSP Video Stream
Connect a real-time video camera using RTSP protocol:CSV File Reader
Connect a CSV file for data streaming:Available Input Stream Types
rtsp_video_streamer
rtsp_video_streamer
Description: Real-time video streaming from RTSP camerasConfiguration Parameters:
rtsp_url(string): RTSP stream URLtarget_image_size(array): [height, width] in pixelstarget_frame_rate_hz(float): Frames per second to process
csv_file_reader
csv_file_reader
Description: Stream data from uploaded CSV filesConfiguration Parameters:
file_id(string): ID of uploaded CSV file
video_file_reader
video_file_reader
Description: Stream a pre-recorded video file (e.g.
.mp4) that was previously uploaded via the Files APIConfiguration Parameters:file_id(string):file_idreturned by the upload — typically the original filename (e.g."my_video.mp4"), not thefile_uidUUID
sensor_streamer
sensor_streamer
Description: Direct sensor data streamingConfiguration Parameters:
- Configuration varies by sensor type
Direct Model Queries
model.query
Send direct queries to Newton models with custom data payloads.model.query is only handled by lenses whose pipeline includes a model-query processor. Lenses that wrap a streaming processor (e.g. lens_camera_processor, lens_timeseries_state_processor) expect input via input_stream.set and will time out on model.query. Responses arrive asynchronously — long-running queries return a timeout response with message: "Response timed out for query" if the model has not replied within the WebSocket read window.Image QA Example
Object Detection Example
Newton Model Templates
Available Templates
- Image QA Template
- Object Detection Template
Template Name: Timeout Response (model did not reply within the read window):
image_qa_template_taskPurpose: Generate narrative descriptions of imagesParameters:instruction: Question or instruction for the modelfocus: Specific aspect to focus on (e.g., “safety equipment”, “project status”)max_new_tokens: Maximum response length (typically 512)
Error Handling
Handle WebSocket errors and connection issues:Best Practices
Connection Management
Connection Management
- Always authenticate WebSocket connections with your API key
- Implement connection retry logic with exponential backoff
- Set appropriate timeouts for requests (30 seconds recommended)
- Close connections properly to free resources
Event Handling
Event Handling
- Use unique
client_idfor each client to enable parallel processing - Handle asynchronous events properly with
session.read - Implement error handling for malformed responses
- Log events for debugging and monitoring
Performance Optimization
Performance Optimization
- Use appropriate image sizes for video streams (360x640 recommended)
- Set reasonable frame rates (0.2 Hz for most use cases)
- Monitor processing times and adjust accordingly
- Batch similar queries when possible
Next Steps
Examples Repository
Explore complete working examples
Session Management
Monitor and manage active sessions
File Upload API
Upload files for CSV streaming
Troubleshooting
Solve common WebSocket issues