Skip to content

Web Messaging with RxJS

If you hide incoming messages behind an observable, the transport between browser and server becomes interchangeable – and you notice along the way how far plain HTTP gets you.

≈ 12 min readSoftwareReactive

Web Messaging with RxJS

var incoming = Rx.Observable.create(function (observer) {
  var source = new EventSource('/subscribe/green');

  source.onmessage = function (event) {
    observer.onNext(JSON.parse(event.data));
  };
  source.onerror = function (error) {
    observer.onError(error);
  };

  return function () {
    source.close();
  };
});

incoming
  .map(function (message) { return message.green; })
  .distinctUntilChanged()
  .subscribe(function (count) {
    render(count);
  });

The interesting thing about this piece of client code is the part that’s missing. The consuming side – the three lines starting at incoming.map(...) – knows nothing about EventSource, nothing about HTTP, nothing about connections. It subscribes to a stream of incoming messages and processes it with the same operators it would use for an array or a sequence of mouse clicks. How the messages technically arrive is locked inside the create callback. This very separation is the core of what I’ve been trying out over the last few weeks: thinking of messages between browser and server not as a connection problem but as a stream – with RxJS as the shared language on both sides.

Why not go straight to WebSockets?#

As soon as someone in a project says “real-time,” reaching for WebSockets is the obvious move – usually in the form of socket.io, which kindly wraps up the connection details. That is often the right choice, but it comes at a price that tends to show up only later. WebSockets are a protocol of their own with their own handshake. Older proxies swallow the upgrade, some corporate firewalls cut long-lived connections without comment, load balancers need special treatment, and the familiar HTTP toolbox – caching, auth headers, access logs, curl for debugging – only partly applies. socket.io answers some of this with fallbacks, but in return pulls a whole library layer into the project on client and server.

Yet the business requirement is often more modest than the word “real-time” suggests: the server should inform the browser about events, mostly in one direction, and a latency of a fraction of a second is perfectly fine. For this profile, HTTP has had answers for years that work without switching protocols: long polling, chunked responses, server-sent events. All of them run through any infrastructure that speaks HTTP – which is practically everything.

So the real architecture question is not “WebSockets or not?” but: How do I write the application code so that this decision can be revised later?

An experiment: everything is a stream#

A little under two weeks ago, I published a small repository on this, web-messaging. The README line describes its scope honestly: “Message routing and Complex-Event-Streams via HTTP (Transfer-Encoding: chunked)”. It’s an experiment, not a product – a single server.js, an index.html, no Express, not even a routing framework, but a hand-written mini router made of regular expressions. This very simplicity makes it visible what it’s all about.

The server models everything that produces messages as an observable. A simulated source ticks every two seconds and rolls a random group; a Subject serves as the entry point for messages from outside. Both flow into a shared stream:

var rx = require('rx');

var publishChannel = new rx.Subject();

var simulated = rx.Observable.interval(2000)
  .map(function (tick) {
    return { sequence: tick, group: randomGroup() };
  });

var events = simulated
  .merge(publishChannel)
  .publish()
  .refCount();

var totals = events
  .scan({}, function (acc, message) {
    acc[message.group] = (acc[message.group] || 0) + 1;
    return acc;
  })
  .publish()
  .refCount();

Two things in it turned out to be load-bearing. First, merge: the fact that a simulated time source and a Subject fed by HTTP POSTs can be combined into one stream without anyone noticing is not a gimmick – it means that “where does the message come from?” stays a local decision. Second, publish().refCount(): without these two calls, the stream would be cold, and every subscriber would get its own simulation with its own counter. This way, all connections share the same hot source, and scan keeps exactly one count across all groups – state as a stream, not as a variable that many places write to.

The publishChannel is fed by the POST route, and that may be the prettiest line in the experiment: an ordinary HTTP request becomes an event in the stream.

route.post(/\/publish\/([a-z]*)/, function (req, res) {
  publishChannel.onNext({ group: req.params[0] });
  res.end();
});

The response as an Observer#

More exciting than the producing side is the delivery. How does a server stream get into a browser without WebSockets? The experiment’s answer: the HTTP response simply stays open. With Transfer-Encoding: chunked, a server may write in small pieces for as long as it likes – and that turns res into an Observer that receives messages from the stream:

route.get(/\/subscribe\/([a-z]*)/, function (req, res) {
  res.setHeader('Content-Type', 'application/json');
  res.setHeader('Transfer-Encoding', 'chunked');

  var subscription = totals
    .map(function (data) {
      var group = req.params[0];
      if (!group) return data;
      var result = {};
      result[group] = data[group];
      return result;
    })
    .subscribe(function (data) {
      res.write(JSON.stringify(data) + '\n\n');
    });

  req.on('close', function () {
    subscription.dispose();
  });
});

Every connection is a subscription. That sounds trivial, but along the way it takes care of the problem that regularly leads to memory leaks in hand-rolled publish-subscribe servers: lifetime. When the browser closes the connection – tab closed, network gone, page reloaded – req.on('close') fires, the subscription ends with dispose(), and because refCount keeps count, the source stops working as soon as the last subscriber is gone. Cleanup is no longer special logic but the natural flip side of subscribing. In a project a few years earlier, we maintained a dedicated registry with timeout heuristics for exactly this cleanup – here it’s two lines.

A side effect of this setup that I wouldn’t want to give up anymore: you can observe the stream with built-in tools. A curl http://localhost:8765/subscribe/green in the terminal, and the messages trickle in line by line – no special tool, no browser plugin, no protocol inspection. The repository even has a dedicated /log route for this, which delivers the unfiltered raw stream before aggregation. While developing, I always had two terminals open: one on /log for the raw events, one on /subscribe/... for what a client actually sees. With a WebSocket setup, the same visibility already requires building your own tools, or at least the frame view of the browser developer tools.

The flow at a glance:

flowchart LR
  POST[HTTP POST<br/>/publish/:group] --> SUBJ[Subject<br/>publishChannel]
  SIM[Interval<br/>Simulation] --> MERGE[merge]
  SUBJ --> MERGE
  MERGE --> SCAN[scan<br/>count per group]
  SCAN --> SUBA[Subscription<br/>connection A]
  SCAN --> SUBB[Subscription<br/>connection B]
  SUBA --> RESA[chunked Response A<br/>res.write]
  SUBB --> RESB[chunked Response B<br/>res.write]

On the client side, by the way, I’m less consistent in the repository: the index.html is a small AngularJS 1.4 application that still picks the chunks out of an XMLHttpRequest by hand – at readyState === 3, cut the responseText at the double line breaks and parse the last complete line. That works, but it’s exactly the kind of transport detail that has no place in application code. The Rx.Observable.create wrapper from the beginning is the consequence: written once, it hides this fiddling, and everything behind it only speaks Observable. If you use server-sent events instead of raw chunks – the same pattern, just formalized as text/event-stream – EventSource even gives you parsing and reconnecting from the browser for free.

Reconnect and backpressure as stream problems#

Two topics decide whether a setup like this stays a weekend experiment or holds up in operation. Both can be phrased as stream problems, and that is the second reason RxJS pays off here.

First, re-establishing the connection. Network connections break – the question is not whether, but how often. With EventSource, the browser reconnects on its own and even sends Last-Event-ID to say where it left off, as long as the server assigns IDs. With the raw XHR approach, you build this yourself – and instead of a loop with state variables, it becomes an operator on the stream:

var resilient = incoming.retryWhen(function (errors) {
  return errors.delay(3000);
});

The error case is no longer a special path with its own code but part of the same declaration as the success case. If you want smarter behavior – growing wait times, giving up after n attempts – you compose it from the same building blocks instead of maintaining a state machine.

Second, backpressure. The server in the experiment writes into the response as fast as the stream delivers. What if the receiver can’t keep up – a mobile device in a dead zone, an overloaded tab? In Node, res.write honestly returns false when the kernel buffer is full; the experiment generously ignores this, and that’s exactly where the fun stops. The clean answer is again a stream answer, because RxJS comes with the tools to shape the rate before it hits the wire:

  • sample sends only the most recent state at fixed intervals – ideal for aggregations like the count here, because nobody cares about outdated intermediate states.
  • bufferWithTime collects messages and sends them in batches – when nothing may be lost but the frequency should drop.
  • pausableBuffered holds the stream until the receiver has room again – real pausing instead of dropping, for example coupled to the response’s drain event.

What matters is less which of the three paths you choose than that the question has an answer in the model at all. In the callback world, “producer faster than consumer” is a cross-cutting problem that runs through the whole code. In the stream, it’s one line in the right place.

The transport becomes interchangeable#

Now to the real architectural gain. If the consuming side only sees an Observable, the transport is an implementation of this interface – and implementations can be swapped. The same application, tomorrow over WebSockets:

var incoming = Rx.Observable.create(function (observer) {
  var socket = new WebSocket('ws://localhost:8765/subscribe/green');

  socket.onmessage = function (event) {
    observer.onNext(JSON.parse(event.data));
  };
  socket.onerror = function (error) {
    observer.onError(error);
  };

  return function () {
    socket.close();
  };
});

The lines from incoming.map(...) on stay unchanged, as does retryWhen and everything built on it. Long polling fits into the same frame: a request that responds when a message arrives, chained into an endless stream with flatMap and repetition. Switching the transport is then a local change in one file – not open-heart surgery on the application.

flowchart LR
  LP[Long polling] --> OBS[Observable<br/>shared interface]
  SSE[Server-Sent Events] --> OBS
  CH[chunked HTTP] --> OBS
  WS[WebSocket] --> OBS
  OBS --> APP[Application code<br/>map / filter / scan / retryWhen]

At this point I noticed that the “WebSockets vs. HTTP” discussion in teams is often so tough because it’s treated as a one-way street – as if the decision had to hold forever. Once the transport sits behind an observable, it loses that weight. You can start with the least demanding transport that meets the requirement and move later when measurements call for it.

When plain HTTP is enough#

That leaves the honest trade-off. After this experiment, I would choose ordinary HTTP – chunked or as server-sent events – wherever three conditions come together:

  • The messages flow overwhelmingly from the server to the browser; the other direction consists of occasional commands, for which a POST is perfectly sufficient.
  • Latencies of a few hundred milliseconds don’t bother anyone – dashboards, notifications, progress indicators, feeds.
  • The existing HTTP infrastructure – proxies, auth, logging, debugging with curl – should keep working without detours.

WebSockets earn their place where these conditions break down: real bidirectional traffic with many small messages in both directions, collaborative editing, games, anything with a noticeable latency requirement. Then one POST per message no longer holds up, and switching protocols pays off. One drawback on the HTTP side belongs on the table too: browsers cap the number of concurrent connections per host, six is common – if you open many streams in parallel, you have to multiplex them into one on the server side. HTTP/2 has been an approved RFC since May and will ease exactly this problem, but in everyday browser use it isn’t yet something you can plan with today.

And of course the experiment remains an experiment: no authentication, no persistence, no message IDs for gap-free catching up after a reconnect, and a message space that exists exactly as long as the one Node process. If you need more, you attach a real source to the Subject – a queue, a log – and that’s exactly what that spot is already prepared for.

Bottom line#

The value of this small server doesn’t lie in the transport trick with chunked responses – that existed long before RxJS. It lies in both sides speaking the same abstraction. On the server, the simulation, the POST input and the aggregation are observables that can be composed with merge and scan; in the browser, the incoming message stream is an observable that map, distinctUntilChanged and retryWhen work on. Connection loss, retries, rate limiting, cleanup – everything that lies scattered across the file as special cases in callback code has a named place in the stream.

ES2015 was approved a few weeks ago, and much of the code above will get prettier with arrow functions. That doesn’t change the core: the decision to model incoming messages as a stream and hide the transport behind it is independent of syntax and, by the way, of RxJS itself – it would work with any Observable implementation. So what I take away from the experiment is less a library recommendation than an order of steps: first define the message model as a stream, then choose the least demanding transport that meets the requirement – and keep the freedom to switch it when reality calls for it.

Further reading#

Revised and translated from German with the help of AI.