A new version of the HTTP protocol, which is HTTP/3, is developed on top of the QUIC protocol. QUIC is a networking protocol that works better than HTTP/2 with the aim of improving speed and performance. When we talk about bidirectional streams, we mean a communication channel that can send and receive data simultaneously in both directions. This type of communication is particularly suitable for use cases such as online chat, live streaming, and real-time web applications.
Using bidirectional streams can reduce delays in sending data, enabling applications to maintain a more stable and faster connection. This means that whenever the client or server needs to send or receive data, this operation will occur without delay or interruption.
A stream in HTTP/3 is a section independent from the QUIC connection that sends and receives data through it. Each stream can send and receive data without needing to establish a new connection, reducing the latency of connection setup time, such as for establishing TCP connections.
The nature of streams in HTTP/3 is very versatile and allows us to maintain simultaneous operations while ensuring security and stability, as well as allowing more data to be sent and received efficiently. In this regard, the bidirectional stream is a key tool that facilitates multiple user interactions similar to those described above.
Some benefits of the HTTP/3 protocol and bidirectional streams include reduced latency, improved performance under unstable network conditions, and reduced overall bandwidth consumption.
Let’s take a look at a practical example of how to work with streams in HTTP/3:
client.on('stream', function (stream) {
stream.write('Hello Server!');
stream.on('data', function (chunk) {
console.log('Received from server:', chunk.toString());
});
});
Line-by-line code explanation:
client.on('stream', function (stream) {This line is for receiving an event whenever a new stream is created on the client.
stream.write('Hello Server!');
This line sends a message to the server with the content 'Hello Server!'
stream.on('data', function (chunk) {
Upon receiving data from the server, this event runs and allows for processing the received data.
console.log('Received from server:', chunk.toString());
A message received from the server is printed to the console.