Skip to main content
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.

Connection Setup

1. Create a Lens Session

First, create a lens session using the REST API:
The response includes a 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 required type field and optional event_data field:
Response envelope: with the single exception of session.status (see below), every server response carries a type field suffixed with .response and wraps the result in event_data:
When event_data would be empty, it is returned as null.

Session Events

session.status

Query the current status of the active session.
Response Example: Unlike other events, session.status returns the full session metadata record (the same shape as Create Lens Session), not the {type, event_data} envelope.
The response currently includes an api_key field that echoes back the caller’s real API key. Treat this field as a secret — do not log, persist, or expose it in client-side code. (Server-side scrub in progress.)

session.validate

Run validation checks on the current session, including health checks and model connectivity.
Response Example:

session.read

Read pending event messages from the platform, such as log messages or asynchronous inference results.
session.read only returns messages for lenses whose model_pipeline writes to the WebSocket mailbox. Lenses that use a server_sent_events_writer output processor (Activity Monitor and most cookbook lenses) route inference results to the SSE consumer endpoint instead — for those, session.read returns event_data: null even when GET /lens/sessions/metadata shows non-zero num_outputs. Inspect the lens’s model_pipeline via GET /lens/metadata to confirm which output channel it uses.
Each client should generate a unique client_id to enable parallel reading. Use a new client_id to reset message stream playback.
Response Example (no pending messages):
Response Example (with pending messages):

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

Description: Real-time video streaming from RTSP camerasConfiguration Parameters:
  • rtsp_url (string): RTSP stream URL
  • target_image_size (array): [height, width] in pixels
  • target_frame_rate_hz (float): Frames per second to process
Use Cases: Security cameras, live monitoring, real-time analysis
Description: Stream data from uploaded CSV filesConfiguration Parameters:
  • file_id (string): ID of uploaded CSV file
Use Cases: Time series analysis, batch data processing, historical data analysis
Description: Stream a pre-recorded video file (e.g. .mp4) that was previously uploaded via the Files APIConfiguration Parameters:
  • file_id (string): file_id returned by the upload — typically the original filename (e.g. "my_video.mp4"), not the file_uid UUID
Use Cases: Offline analysis of recorded footage, replaying dashcam / camera archives through a vision lensSee Video File Reader for the full data-connector reference.
Description: Direct sensor data streamingConfiguration Parameters:
  • Configuration varies by sensor type
Use Cases: IoT sensors, accelerometers, temperature sensors

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

Template Name: image_qa_template_taskPurpose: Generate narrative descriptions of imagesParameters:
  • instruction: Question or instruction for the model
  • focus: Specific aspect to focus on (e.g., “safety equipment”, “project status”)
  • max_new_tokens: Maximum response length (typically 512)
Output: Natural language description, wrapped in the WS response envelope.Example Response:
Timeout Response (model did not reply within the read window):

Error Handling

Handle WebSocket errors and connection issues:

Best Practices

  • 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
  • Use unique client_id for 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
  • 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