Real-time + Postgres = ?

Real-time updates are part of almost any modern web and mobile application, from banking apps to news feeds. There are so many ways to handle them that there will always be debate over which one is best.
A Bit of History
Over the last 10 years, the most common approach has been to use a websocket client and server, like socket.io, and send events to clients through it by firing them from the backend.
There are also more recent data-based solutions, like RethinkDB. I really like this approach because, from my point of view, it should be changes in the data that trigger events: clients subscribe to a specific query and their data updates accordingly. That's what RethinkDB offers: good client libraries, its own query syntax, and scalability. Unfortunately, the RethinkDB project shut down and is now only maintained by the community.
GraphQL is another promising and more recent development, but its early versions didn't have subscriptions, so it remains to be seen how far it will go. GraphQL follows an approach similar to RethinkDB's: it's an effective, self-contained, and scalable solution that lets you request data directly from the client, which works great for replacing backend API calls.
Today GraphQL already supports real-time subscriptions, but there's still the problem that it works as a layer separate from the database. In many cases that's not an issue, but it's not clear what PostgreSQL users are supposed to do. Not every company has the resources to migrate to GraphQL, especially if they have a huge product with a lot of logic at the database level.
This problem came up on a project that needed a simple way to implement real-time updates without giving up PostgreSQL. And that's where Hasura comes in! Finding it on Product Hunt was a joy, because it was the solution I'd been looking for this problem for about three years.
What Is Hasura?
Hasura is a proxy layer between your PostgreSQL database and a GraphQL server that lets you set up real-time updates from PostgreSQL very quickly, and it's also open source. You get all the advantages of GraphQL without giving up access to your original database: client libraries for web and mobile, query syntax, and subscriptions.
Hasura's Key Features
Real-time updates from your PostgreSQL database When I saw how easy it was to set up, I was impressed.
Dynamic Access Control and Authentication
If you use Auth0 as your authentication provider, Hasura lets you restrict data access via JWT. Plus, Hasura has a really good and simple UX for configuring access rules from the interface. Check out this video to see it in action.
For example, you might have several tables that you use on the client side. You can set it up so a user can only access data if they own it, and define whether they have read, write, or delete permissions.
Triggers on Database Events
You can subscribe to every create, update, and delete action in your database and call another service, passing that data along. One possible use case is using it as a communication channel between microservices or to call serverless functions.
The release of this feature was great news: before, the alternative was to use PostgreSQL's native subscriptions, but maintaining that required a lot of code and there was no central, bulletproof solution.
What's really interesting is that it's a very reliable solution. Whatever happens to Hasura or the network connection, the event will be delivered as soon as it's restored. The same goes if, due to some unforeseen issue, changes reach PostgreSQL through backend API calls but another part of your application is down.
WS to HTTP Fallback
This isn't exclusive to Hasura, but when you use GraphQL subscriptions and your client doesn't support WebSockets, HTTP is automatically used as a fallback without you having to do anything else.
Of course, there are other features you might find useful, like data mutations and custom resolvers.
Practical Experience
Hasura is a genuinely good product that can save a ton of time. It still doesn't have a stable release and is still under development, but it can already be used in production, because its core features work reliably. Almost every week there's a new update, and the developers are getting closer to v1.0.0.
On the project mentioned above, migrating to Hasura took only two weeks: one for development with a single developer, and one for bug fixing and QA. The result was a good, sustainable solution that works for both web and mobile apps. The next steps involved using more event triggers and migrating to serverless.
You can try Hasura for free right now with the one-click deploy to Heroku from the link on their website. If you want to know more, another good topic for a future article would be how to set it up on top of an existing PostgreSQL database and use it with a React/Redux application.