Urgent.News

What's breaking now, across thousands of outlets.

Tech

Mocking SSE: Streaming Events and How a Client Handles a Dropped Connection

Quick answer: Mocking a Server-Sent Events endpoint is not the same as returning a list. The value under test is the stream over time: the framing of each event, the client's behavior mid-connection, and what it does when the connection drops and retries. A good SSE mock lets you control timing and injection of faults so you can verify those transitions instead of hoping they work in production.…

Mocking a Server-Sent Events (SSE) endpoint requires a deeper understanding than simply returning a list. The key aspects to focus on are the framing of each event, the client's behavior during mid-connection, and how it handles connection drops and retries. A robust SSE mock should allow you to control the timing of events, inject faults, and simulate various scenarios to ensure the frontend correctly reacts to these events.

In a practical example, consider a job-progress UI that updates a progress bar based on events emitted by a backend during a long import process. Initially, using a mock that returns a JSON array of events caused the UI to render perfectly, as all events arrived simultaneously. However, in a real scenario, the stream could stall for several seconds between events, causing the browser to drop the connection and trigger the client's retry logic, which could result in erroneous event processing, such as the progress bar jumping backward. This type of issue would go unnoticed with a static JSON fixture.

The distinction between SSE and a regular HTTP response is crucial: SSE utilizes a long-lived HTTP connection with a specific framing. Each event is a block of lines ending with a blank line, and the connection remains open. The client automatically re-establishes the connection upon a drop and sends the last received event ID via the Last-Event-ID header.

To accurately test SSE behavior, a mock must let you control the event delivery schedule, simulate idle gaps, and inject delays to test behaviors like keep-alive and spinner updates. It should also allow you to close the connection mid-stream and verify the client's retry mechanism.

Building a time-aware mock involves using the web streams API in Node.js, which provides control over when events are pushed. This allows you to schedule events at specific intervals, representing real-world delays and idling periods. When simulating a dropped connection, the mock should close the stream and allow the client to reconnect automatically.

The test should then verify that the client resumes the stream correctly, starting from the last processed event ID, without reprocessing any events or getting stuck in an infinite loop.

Additional considerations include sending periodic comment lines as keep-alives to prevent intermediaries from closing idle connections prematurely. The mock should also handle explicit error events and ensure the UI correctly differentiates between recoverable interruptions and terminal failures. By focusing on these aspects, a mock can faithfully replicate the real streaming contract, enabling thorough testing of edge cases that are critical for a smooth streaming user experience.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Profiling VR Performance on Target Hardware: Measure Before You Optimize

To find out why a VR build drops frames, measure it on the target headset first, work out whether you are missing the frame budget at all, then classify the bottleneck as CPU, GPU, memory or thermal…

  • Measure VR performance on target headset before optimizing
  • Identify CPU, GPU, memory or thermal bottleneck first
  • Implement changes one at a time with regression check

A Repeatable Map to 3D Pipeline for Site Context (and Where Each Tool Fits)

I needed site context for a 220 acre warehouse district study, and I wanted a pipeline I could rerun on the next site without relearning five interfaces.

  • Five-step pipeline developed for repeatable site context creation
  • Each tool in pipeline has specific role, none produces final model
  • Initial steps should be done cheaply and thoroughly checked

Your API Works. But Is It Actually Reliable?

Building a REST API is easy. Building one that handles things going wrong? That's where it gets interesting. When I first started working with APIs, I mostly cared about one thing: Does the endpoint…

  • Utilize appropriate HTTP status codes for clear communication
  • Validate user input to prevent database corruption
  • Implement custom error handling to maintain API stability

The Uninstall That Actually Works: A Practitioner’s Guide to Cleaning Up Windows Apps

The Uninstall That Actually Works: A Practitioner’s Guide to Cleaning Up Windows Apps I’ve lost count of how many times a “simple” uninstall left me with a broken PATH, a zombie service, or a registry…

  • Perform inventory of app footprint before uninstalling
  • Run official uninstaller using appropriate method based on app type
  • Rename leftover files to .bak extensions for safekeeping

More from Monday 2 November →