We all work with web resources. What if we had a nice way to know when they change?
LiveResource is a protocol specification and JavaScript reference library for receiving live updates of web resources. It has the following principles:
- Blend naturally with RESTful API design.
- Optionally support WebSockets, but do so without abandoning established API principles.
- Keep it simple. It should be possible to
curl
for updates.
LiveResource differs from other realtime push solutions by providing a client-side interface modeled around synchronization rather than messaging or sockets. Here’s an example using the JavaScript client library:
How it works
The client makes a GET or HEAD request to discover the capabilities of a resource:
For example, the value-wait
link indicates that it’s possible to make a GET request that hangs open until the value changes (this is known as long-polling). The multiplex-socket
link is for WebSocket connectivity. Versioning is handled using ETags.
What’s nice about LiveResource’s long-polling mechanism is that it is simple and stateless. It’s just a GET request with If-None-Match
and Prefer headers:
There is no complicated socket session emulation. This means you can even use curl
:
Of course, LiveResource’s WebSocket mechanism can be used for greater network efficiency.
If a resource doesn’t advertise any push capabilities, then the client will resort to plain polling. This means that a LiveResource client is compatible with any resource on the Internet, and automatically becomes more efficient if push capabilities are discovered.
Protocol
See the full spec here.
Test server
There is a test server at test.liveresource.org
. You can use it to play around with the protocol or test client code against. For example, there is a resource that reports the current time.
Q & A
-
What about WebSockets?
The LiveResource protocol supports WebSockets as one of its push mechanisms.
-
What about Webhooks?
The LiveResource protocol supports Webhooks as one of its push mechanisms, following the recommendations outlined by REST Hooks.
-
What about HTTP/2?
LiveResource is intended to be efficiently usable with HTTP/2. In fact, we believe LiveResource’s SSE mechanism in combination with HTTP/2 could eventually supplant the need for WebSockets.
-
Can I use LiveResource to send data to the server?
Nope. But you weren’t having this problem, were you? Just use normal HTTP for that.
-
Does LiveResource require a certain database backend?
No. LiveResource doesn’t involve the database at all.
-
Does LiveResource require building the front end as a Single Page App?
Of course not.
-
Does LiveResource require me to write all of my code in JavaScript?
No! We aim to have libraries in many languages, both server and client.
-
Does LiveResource come with a package management system?
Go home. You’re drunk.