Stream Browser Sessions with a Session Replay API
In this article
Streaming browser sessions with a session replay API lets you watch a user's clicks, scrolls, and navigation in real time or near-real time, as if you were looking over their shoulder. This is useful for live support, debugging user-reported issues, or monitoring high-value user flows. You'll need a session replay service that offers a streaming endpoint (most major providers like FullStory, LogRocket, or OpenReplay do), and you'll need to have the replay SDK already installed on your site so sessions are being captured.
What You'll Need
- A session replay service account with API access (check your provider's docs for the streaming endpoint).
- The session replay SDK installed on your website, capturing sessions.
- An API key or token with permissions to read sessions (usually found in your provider's dashboard).
- A way to make HTTP requests (like a terminal with curl, or a small script in Node.js/Python).
- Optional: a WebSocket client if your provider streams over WebSocket (most do).
Step-by-Step
- Identify the session you want to stream. You need a session ID. You can get it from your provider's dashboard, or via their API by filtering by user ID, URL, or time. For example, in LogRocket you'd call
GET /api/1/sessions?user_id=123. - Find the streaming endpoint in your provider's API docs. Look for terms like "replay stream," "live session," or "WebSocket endpoint." For OpenReplay, it's
wss://app.openreplay.com/wswith a session ID parameter. - Authenticate your request. Most providers require an API key in the header (e.g.,
Authorization: Bearer YOUR_API_KEY) or as a query parameter. Check the docs for the exact format. - Open a WebSocket connection to the streaming endpoint. If you're using Node.js, you can use the
wslibrary. Here's a minimal example:const WebSocket = require('ws'); const ws = new WebSocket('wss://api.provider.com/stream/session/123?token=YOUR_TOKEN'); ws.on('message', data => console.log(JSON.parse(data))); - If your provider uses HTTP polling instead of WebSocket, call the endpoint repeatedly (e.g., every 500ms) with a
sinceparameter to get new events. Each response will contain a batch of events since the last poll. - Parse the incoming events. Events typically include types like
click,scroll,input, andnavigate. Each event has a timestamp and coordinates or element selectors. You can log them or feed them into a visualization tool. - Handle the stream's lifecycle. The stream will send a
closeorendevent when the session ends. Make sure your code cleans up the connection and handles reconnects if the network drops. - Test with a live session. Open your own site in another browser tab, trigger a few actions, and confirm you see those events in your stream output.
Common Mistakes
- Using the wrong session ID. Make sure you're using the provider's session ID, not your internal user ID or a random string.
- Forgetting to check if the session is still active. If the session has already ended, the stream will immediately return an error or empty. You can only stream live sessions.
- Not handling rate limits. Polling too frequently can get you throttled. Use the provider's recommended interval (often 1-2 seconds).
- Ignoring the event schema. Each provider has a different JSON structure. Don't assume it matches another service.
- Not closing the WebSocket properly. Leaving connections open can exhaust your API quota or cause memory leaks in your app.
Tips
- Start with a small script that just prints events to the console before building a full UI.
- Use a library like
socket.io-clientif your provider uses Socket.IO instead of raw WebSocket. - For debugging, add a timestamp to each event and log the time delta between events to spot lag.
- If you need to replay the stream later, save the raw events to a file or database as you receive them.
- Check if your provider offers a "live" mode that includes mouse movement, which is more data-intensive but more useful for support.
Troubleshooting
- Connection closes immediately: The session ID is likely wrong or the session has ended. Double-check the ID and try a fresh session.
- 401 Unauthorized: Your API key is invalid or lacks the
readpermission. Regenerate the key and ensure the scope is set. - No events coming through: The session might be idle. Ask the user to move their mouse or click something. Also verify that the SDK is actually recording (check your provider's dashboard for the session).
- Events come in bursts: That's normal if the provider batches events. Adjust your UI to handle a few seconds of silence followed by a burst.
- WebSocket connection fails with CORS error: If you're testing from a browser, the provider may block cross-origin requests. Use a server-side script instead.
Frequently Asked Questions
Can I stream a session that already ended?
No, streaming is only for live sessions. For ended sessions, you'd fetch the full replay data via a separate API endpoint.
How much latency is typical?
Most providers have a delay of 1-3 seconds between the user's action and when it appears in the stream, due to batching and network.
Do I need to install a separate SDK for streaming?
No, the same session replay SDK that records sessions also sends the live stream data. No extra installation is needed.
Can I stream multiple sessions at once?
Yes, you can open multiple WebSocket connections, but be mindful of your API rate limits and your own server's resources.
What if the user's browser is offline?
The SDK will buffer events locally and send them when the connection is restored. The stream will appear to pause and then catch up.
Is streaming available on all plans?
Some providers restrict live streaming to higher-tier plans. Check your provider's pricing page or contact support.