In Q2 this year, Directus released WebSockets, which lets you build real-time features into projects that are backed by Directus-managed databases.
WebSockets provide a standardized protocol for bidirectional communication channels over a single connection, making it possible for web apps to update in real time.
With support for WebSockets, Directus users can connect projects with code and have apps automatically and instantaneously reflect updates to data – without having to manually refresh or repoll.
It’s a pretty cool feature for apps that rely on real-time data, such as dashboards, chat applications, online gaming, live streaming, collaborative tools, financial trading platforms and tons of others. Not only does it save developers’ time, it improves the user experience.
Of course, WebSockets aren’t new – they’ve been around since 2011. They were a big advancement in web development at the time, replacing the inefficient and less-responsive method of using HTTP requests to check the server for updates.
While WebSockets have continued to evolve and gain support across different programming languages and frameworks, so has HTTP – and many wonder whether HTTP/2 will replace them.
Let’s have a look at that topic.
Low-latency Data Exchange
HTTP/2 is primarily focused on optimizing the delivery of web content and improving the performance of web applications.
It does this by enhancing the efficiency of HTTP requests and responses. WebSockets, on the other hand, is designed for real-time, bidirectional communication – the kind that’s required for use cases where persistent, low-latency connections are required.
HTTP/2 offers several advantages over HTTP/1.1, which result from faster, more efficient data exchange between clients and servers:
-
Features like multiplexing, header compression and prioritization help to reduce latency and speed up page rendering.
-
HTTP/2 employs header compression, which significantly reduces the overhead of transmitting headers with each request and response, which is beneficial in situations where bandwidth is limited.
-
HTTP/2 introduces server push, which allows the server to proactively send resources to the client, which reduces the need for round-trip requests, further accelerating page load times.
Additional features include improved security, better mobile performance and research efficiency. Many websites and web applications have migrated to HTTP/2 to benefit from these enhancements.
Is WebSockets better?
HTTP/2 and WebSockets serve different purposes, and each has distinct advantages.
They’re not necessarily better or worse than each other – depending on your application's requirements, you may choose to use one or both of these technologies in different parts of your web application architecture.
Here are some of the things we like about our implementation of WebSockets:
-
Persistent Connection: Just like HTTP/2, WebSockets enable the server to proactively send data, without waiting for the client to request it. But WebSockets maintain a persistent connection between the client and server, so they’re well-suited to applications that require continuous data streaming.
-
Full Duplex Communication: WebSockets are designed for full-duplex, bidirectional communication, enabling real-time, low-latency data exchange for applications requiring constant data updates.
-
Custom Protocols: WebSockets allow you to create custom protocols for specific communication needs, as well.
-
Flexibility and Scalability: WebSockets is built to handle high levels of concurrency and can scale to support a growing user base, whether for a small application or a large-scale platform.
To sum it up, while HTTP/2 is primarily focused on optimizing the loading of web resources and improving the performance of traditional web applications, WebSockets are optimized to support real-time, bidirectional communication.
In some cases, you may even use both technologies in a single application, with HTTP/2 for loading web pages and WebSockets for real-time data updates.
Never be out-dated
Real-time data access has become the expectation in all aspects of life and business.
With the Directus implementation of WebSockets, you can send, create, update and delete operations over a single WebSocket connection – and be confident that the data that’s powering your applications is always up-to-the-minute.
WebSockets is available to all Directus users, whether self-hosting or using Directus Cloud.
For more information on how to get started in 10 minutes or less, visit https://directus.io/ or watch this video tutorial.