Learn about RSocket

Tram Ho

What is RSocket?

RSocket is a new communication protocol that addresses the problems that the current HTTP protocol faces. RSocket is a binary protocol used to transport byte streams such as TCP, WebSocket and Aeron. So what are the limitations of the HTTP protocol?

A modern architecture must support other communication models such as streaming and fire-and-forgot. HTTP (hypertext transfer protocol) is a text-base protocol useful for fetching documents over the internet, but for data centers this protocol is not effective. And the main problem is that HTTP is not reactive. Check out the FAQ section of HTTP / 2

Rsocket is a message-base protocol and requires a few lower-level transport protocols to carry messages. Rsocket define 4 types of communication:

Terms in RSocket:

Frame:

Frame is a single message containing 1 request, response, or protocol processing.

Fragment:

As part of the message application, part has been included in the frame. See more about fragmentation and reassembly here

Transport:

The protocol for carrying the Rscoket protocol, this can be Websocket, TCP or Aeron

Stream:

Stream is understood as a calculation unit (can be request or response or …)

Request:

As a stream request, can be one of the 4 types I mentioned above

Payload:

A stream message (upstream or downstream). Contains data associated with a stream created by a previous request. In Reactive Streams and Rx this is the ‘onNext’ event.

Complete:

1 event was sent to the stream to notify the completion of the success. In Reactive Streams and Rx, what is an onComplete event?

Client:

The side initiating a connection.

Server:

The side accepting connections from clients.

Connection:

The instance of a transport session between client and server.

Requester:

The side sending a request. A connection has at most 2 Requesters. One in each direction.

Responder:

The side receiving a request. A connection has at most 2 Responders. One in each direction.

Data And Metadata

RSocket provides a mechanism for the application to distinguish whether payload is data or metadata. Data and MetaData features:

  • Metadata can be encoded registering than Data.
  • Metadata can be “attached” (ie correlated) with the following entities:
    • Connection via Metadata Push and Stream ID of 0
    • Individual Request or Payload (upstream or downstream)

Framing

Transport Protocol:

RSocket protocol uses transport lower level protocol to carry RSocket frames. A transport protocol must provide the following information:

  1. Unicast Reliable Delivery .
  2. Connection-Oriented and preservation of frame ordering. Frame A sent before Frame B MUST arrive in source order. ie if Frame A is sent by the same source as Frame B, then Frame A will always arrive before Frame B. No assumptions about ordering across sources is assumed.
  3. FCS is assumed to be in use either at the transport protocol or at each MAC layer hop. But no protection against malicious corruption is assumed.

Framing Format:

When the transport protocol provides framing, the RSocket frame is packed into the transport protocol message:

When using a transport protocol that does not provide a compatible frame, LENGTH frames must be added to the RSocket frame.

  • Frame Length: (24 bits = max value 16,777,215) Unsigned 24-bit integer representing the length of Frame in bytes. Excluding the Frame Length field.

Frame Header:

All RSocket Frames start with the frame header, the frame header will include Stream ID, Frame Type, and flags information. 2 Flag (I) gnore and (M) etadata will always be present

  • Stream ID : (31 bits = max value 2 ^ 31-1 = 2,147,483,647) Unsigned 31-bit integer representing the stream Identifier for this frame or 0 to indicate the entire connection. Transport protocols that include demultiplexing, such as HTTP / 2, MAY omit the Stream ID field if all parties agree. The means of negotiation and agreement is left to the transport protocol.
  • Frame Type : (6 bits = max value 63) Type of Frame.
  • Flags : (10 bits) Any Flag bit not specifically indicated in the frame type should be set to 0 when sent and not interpreted on reception. Flags generally depend on Frame Type, but all frame types MUST provide space for the following flags: (I) gnore: Ignore frame if not understood (M) etadata: Metadata present

Connection Setup:

The client must connect to the server to set up a connection. When the connection is established, it will send the SETUP frame. Assuming the transport protocol is TCP, the SETUP frame will look like the following (if not reused):

Request / Response:

Request / Response is the most popular interactive model, but for RSocket as well as Reactive Stream, this is a special case of Request / Stream – in which the response stream has only 1 element or 1 frame. Requester sends 1 request frame and responder reply 1 stream of one frame

The request frame has a Stream ID and a frame type (in this case, REQUEST_RESPONSE). If the client initiates stream ID, it is an odd number and begins with 1 for the first stream, the server uses even stream IDs starting with 2. The following is an example of request / response using TCP. The client is a requester sending a “Hello World!” for server and server to send that message.

And here is the response from the server:

Demo

The theory above seems confusing and headache. Ok now I try the Request / Response demo (Using Spring boot)

Create Project:

Create a normal Spring boot project like any other project, pay attention to some information below:

Dependency

Here I use lombok for fast, and must have dependency is rsocket

application.properties:

Define port for rsocket to connet to

Controller:

Here I create a RsocketController controller, the endpoint is the request-response receives a String request and returns a String Hello + request.

Result:

OK, that’s it. To start the demo request response, start the project: ./mvnw clean package spring-boot:run -DskipTests => Result:

=> Ok, Port 7000 has started, now I need to send a command to port 7000 using Rsocket CLI: To use Rsocket CLI, you clone the jar file here about it:

Then cd to the folder that downloaded the jar file and call the endpoint request-response with the data request as the rsocket client :

=> The result will look like this:

Looking at the request section you will see Frame => Stream ID: 1 Type: REQUEST_RESPONSE Flags: 0b100000000 Length: 40 , the type here is REQUEST_RESPONSE And the data request is an rsocket client string rsocket client :

Receive a response whose type is Type: NEXT_COMPLETE , and data is Hello rsocket c and lient :

Explain

OK, now I explain it a bit here: Rsocket client (rsc.jar download above) will send a request message to RsocketController using RSocket message protocol. The message is sent via TCP to the address localhost:7000 where the server has been deployed. A routing routing instruction will be sent in the first message frame (corresponding to the –route option) it will send request-response

Spring will use this routing information to select the correct @MessageMapping endpoint to call, in this case the requestResponse(String request) . This method will respond to a String, and the CLI client prints all interactions in the terminal, a series of message frames

Summary:

OK, here I just want to introduce about RSocket, and a small demo to understand, There are some terms and definitions that I don’t know how to translate so I understand the original. ? . You should look to the source page to read for ease In the next article I will demo the connection types of RSocket on the application layer instead of going into this.

Refer:

https://rsocket.io/docs/Protocol.html https://dzone.com/articles/an-introduction-to-rsocket

Share the news now

Source : Viblo