Reliable Synchronization of Web Clients: Introducing Resgate

By -

REST APIs are nice. Simple. Stateless. Scalable. But nowadays, your reactive web client’s view (React / Vue.js / Angular) is often expected to be updated whenever the data changes. No more F5 reloading.

As a result, developers tend to add an event stream to send events whenever something changes. But then you have two channels sending information regarding the same data, which is hardly optimal. And this is where the fun starts:

Wolf Questioning

How to synchronize the REST API with the event stream?
How to handle lost events/connections?
How to do selective event distribution (only sending events each client needs)?
How to control access to events (only sending events each client is authorized for)?
How to create search or pagination queries with real-time updates?
How to make it scalable?
How to make it simpler?!

Being the lead developer of the cloud offering at a leading provider of contact center solutions, I had to deal with these issues. And I’ve seen multiple other companies trying to solve the same problem as well. Let me introduce a new open-source solution.

Resgate - a Realtime API Gateway

Resgate is a Realtime API Gateway written in Go. It consists of a single executable file that requires little to no configuration. Taking advantage of Go’s concurrency model, it is highly performant with event latencies <1ms. By acting as a bridge between the web clients and your service (or microservices), it will fetch resources, forward method calls, and handle events, while also providing access control, synchronization, caching, and more.

Clients connect to Resgate using either HTTP or WebSocket, to make requests. Resgate in turn keeps track of which resources each client has requested, keeping it reliably updated for as long as needed.

Services (illustrated by Node.js and Go1 below) will listen for requests over a NATS server, a highly performant messaging system. NATS acts as service discovery (or rather makes it redundant), and enables near limit-less scaling. In essence, it is a simple, efficient, publish/subscribe message broker with request/response capabilities.

RES Network Diagram
A basic setup with Resgate, NATS server, and two microservices.

Writing a service

You write a service pretty much as usual, using the request/response pattern we all know from HTTP. But instead of listening for HTTP requests, the requests will come over NATS. With NATS, the service gets a decoupled way to send events whenever something happends to the data, without giving up other desirable REST traits:

No framework requirements. No database requirements. No language requirements.

Below is a JavaScript (Node.js) example showing how to serve a resource, example.model, using nats client:

var nats = require('nats').connect('nats://localhost:4222');
var model = { message: "Hello, World!" };
// Listen to get requests over NATS
nats.subscribe('get.example.model', (msg, reply) => {
	nats.publish(reply, JSON.stringify({ result: { model }}));

And updating the resource is as simple as sending an event:

// Updating the model
model.message = "Hello, Resgate!";
nats.publish('event.example.model.change', JSON.stringify({
	values: { message: model.message }

Wolf match maker

That’s it!

Now, let’s take a look at the client side.

Writing a client

Fetching data from Resgate can either be done though HTTP, in which case it acts pretty much like an old school REST API:

GET: /api/example/model

{ "message": "Hello NATS" }

Or it can be done using ResClient, a JavaScript library that gets both data and real-time updates through WebSocket:

let client = new ResClient('ws://');
client.get('example.model').then(model => {
	console.log(model.message); // Hello, World!

When using ResClient, you get the additional perk of having the data updated in real-time!

let changeHandler = function() {
	console.log(model.message); // Hello, Resgate!
// Subscribe to changes
model.on('change', changeHandler);
// Unsubscribe to changes'change', changeHandler);

No extra code is needed for updating the model on events that modifies the state. The resources are updated automatically by ResClient.


Wolf relaxing

With Resgate you create REST and real-time APIs with the same effort of doing a REST API the traditional way. The main benefit is having your data synchronized seamlessly across your reactive web clients.

If you are interested in knowing more, the website has plenty of guides, examples, live demos, and more to explore.

  1. The Go gopher was created by Takuya Ueda and designed by Renee French.