Monday, June 06, 2022

RSocket concept

 If you worked with clients and servers architecture, you might be knowing socket. Socket is a familiar term which uses the TCP protocol to communicate with different servers or clients. This is in terms of reactive systems.

Please refer to my old post to know and understand the concept of Websocket in java and reactive programming behaviors.

When we talk of socket/websocket we generally talk with duplex mode of talking i.e a dedicated connection is build between both client and server and message can be sent from any party. This connection remains as it is unless and until any one party break it. This socket/websocket work on top of TCP protocol.

In REST fachione we use http protocol to connect from client to server and server response to the request from client and then connection is closed. This opening and closing the connection is too heavy operation and cant be taken lightly.

So in this article we are going to see what is Rsocket and why we need Rsocket we are also going to see when to use Rsocket concept.

what is Rsocket?

Rsocket is a binary open source protocol which was created by netflix initially and it’s now open sourced. netflix used Rsocket for client server communication within their microservices architecture.

The main difference between Rsocket and the traditional TCP or websockets are Rsockets add more flexibility and gives more upper hand to the developer to deal with the connection and data and it also adds reactive streams behavior to our applications.

As said above in a traditional http request and response, opening a connection and closing the connection is too costlier and cumbersome .. that’s why people go towards websockets but again websocket is protocol over top of HTTP protocol but with Rsockets we have websocket concept along with Reactive stream behavior.

Why we need RSocket?

There are different ways of communication between client and server. Lets first try to understand these concepts.

1- request and response :- this is a traditional http kind of request where we just send a request and then we get a response, and we close the
connection.so what happens in a traditional client server communication is client send a message and the server responds to us with a response and this is similar to how you process our messages using rest apis where you just send a request, and then you get a response over http.Rsocket supports this type of REST request and response communications.

2- request stream :- where you can request the server for a particular type of message and the server will now respond with multiple messages and these could be streaming as well. Best example is you send message as health check to the server and server send you the response of your CPU health check every 2 sec as a stream contineously. This is similar to what we saw in Springwebflux where we get the response in stream as soon as the data is sent from the server we don’t wait for complete response to be filled and then return to the client but server send even a single data as a stream response to the client. Rsocket support this too.

3- fire and forget: fire and forget is useful when you want to do subscription but don’t want the response. for example client say send notificaiton but dont wait for the server to confirm the notification is sent. RSocket can implement this too.

4- Channel or bidirectional communication :- in the bidirectional communication both the server and the client can request and respond, for example let’s say the client said hi to the server and then server responded with a hello and now the server calls back the client saying what now you want me todo, so this also could be possible within the channel or the bidirectional model. Rsocket supports all these four different types
of models.

With Rsocket we can develop your client and server contract in such a way that you can have a mixture of all these now when do we need all these right.

when do to use Rsocket?

Tradition REST is good but when it comes to have a real time data notification we have to either use websocket, websocketrtc or websockettransport. But these are not reactive in behavior they give duplex or bidirection flow to get the benefit of reactive we should use rsocket. best example i remember is of stock market or gaming score or drawing board or chatting application where in many users are connected with each other and using different device i,e one with browser and other with app in such case change by one application need to be informed to the change in anther application this can be done with Rsocket. Other usecase can be twitter like application when server send the info to the client new message has arrived ASAP the message comes. Another usecase can be live-streaming of video as we see in youtube.

In next article we will see code example using RSocket.

Note:- When i started learning reactive behavior i got confuse with many concept i.e. what is reactive programming, what is websocket, what is springwebflus, what is Rsocket, what is socket.io etc so i would suggest you to go in sequence shown below to understand this concept in fast and better way

1- Learn Reactive programming and its implementor like RxJS for ui , RxJAVA for java , Project reactor and JDK 9 comes with stream api.
2- Learn Spring webflux (which implement project reactore which in term use reactive programming concept) use case youtube like strem app.
3- Learn websocket – a protocol that sit on http protocol. (which has nothing to do with reactive behavior)but shown you how to get the real time data use case can be gaming score board, drawing or dashboard, chatting, share market, twitter like app. Extended protocol like websocketrtc and websocettransport.
4- RSocket :- this is protocol that give above explain all four behavior of client-server communications.

5- Socket.io :- this is JS library on top of websocket concept. It gives additional benefits such as Broadcasting, loadblancing, Autoconnect, Fallback, Scaling.

No comments: