Skip to main content

Overview

Tzafon provides real-time streaming capabilities for monitoring browser sessions, receiving live updates, and managing session lifecycle. These features use Server-Sent Events (SSE) for efficient one-way streaming.

Event Streaming

streamEvents()

Stream real-time events from a computer session. Receive updates about actions, page changes, and session state.
# Using the direct API for event streaming
import requests

url = f"https://api.tzafon.ai/v1/computers/{computer_id}/events"
headers = {"Authorization": f"Bearer {api_key}"}

with requests.get(url, headers=headers, stream=True) as response:
    for line in response.iter_lines():
        if line:
            print(line.decode('utf-8'))
Event Types:
  • Session state changes
  • Action completion notifications
  • Error events
  • Page navigation events
Use Cases:
  • Real-time monitoring dashboards
  • Progress tracking for long-running tasks
  • Debugging and logging
  • Building reactive UIs

Screencast Streaming

streamScreencast()

Stream live screencast frames from a browser session. Frames are delivered as base64-encoded JPEG images via SSE.
import requests
import base64

url = f"https://api.tzafon.ai/v1/computers/{computer_id}/screencast"
headers = {"Authorization": f"Bearer {api_key}"}

with requests.get(url, headers=headers, stream=True) as response:
    for line in response.iter_lines():
        if line:
            # Parse SSE data
            data = line.decode('utf-8')
            if data.startswith('data:'):
                frame_data = data[5:].strip()
                # frame_data is base64 JPEG
                image_bytes = base64.b64decode(frame_data)
                # Process or display the frame
Frame Format:
  • Base64-encoded JPEG images
  • Delivered via SSE data: events
  • Typical frame rate depends on activity
Use Cases:
  • Live browser viewing
  • Session monitoring
  • Recording automation runs
  • Building browser preview UIs

Session Management

keepAlive()

Extend the timeout for an active session. Call periodically to prevent automatic termination due to inactivity.
# Keep session alive
result = client.computers.keep_alive(computer_id)
print(f"Session extended: {result}")
Behavior:
  • Resets the inactivity timer
  • Returns current session status
  • Fails if session already terminated
Use Cases:
  • Long-running automation tasks
  • Interactive sessions with pauses
  • Preventing timeout during user input
Call keepAlive() periodically (e.g., every 30 seconds) for sessions that may have long idle periods between actions.

WebSocket Connection

connectWebsocket()

Establish a WebSocket connection for bidirectional real-time communication.
# WebSocket URL format
ws_url = f"wss://api.tzafon.ai/v1/computers/{computer_id}/ws?token={api_key}"
Use Cases:
  • Two-way communication with sessions
  • Real-time control interfaces
  • Low-latency interactions

Best Practices

SSE and WebSocket connections can drop. Implement reconnection logic with exponential backoff.
Screencast frames arrive rapidly. Buffer or throttle display updates to avoid overwhelming the UI.
Always close SSE/WebSocket connections when done to free resources on both client and server.
For automation that includes waiting for external events, call keepAlive periodically to prevent session timeout.

API Reference

EndpointMethodDescription
/computers/{id}/eventsGETSSE stream of session events
/computers/{id}/screencastGETSSE stream of screencast frames
/computers/{id}/keepalivePOSTExtend session timeout
/computers/{id}/wsGETWebSocket connection

Next Steps