Nowadays, Web Applications are set to target maximum relevant users as they can. In order to do so, an Application must comprise of qualities such as fully functional features, easiness, reliability, and last but not least “Performance and Speed”.
Performance and speed are considered as most important because nowadays in this hasty world people need results of their actions in real-time. Suppose you are in a chat room chatting with your friends and you don’t get a reply from them in time even if you have fully working internet connection how would you feel? Definitely, you would not like that.
In order to counter real-time communication problems, the concept of publisher-subscribers is introduced in web applications to built reliable applications.
In the Publisher-subscriber pattern, there are three main components:
1) Publisher :
One who sends or publishes the messages, notification or any sort of information.
2) Subscriber:
The one who is supposed to receive information sent by publisher.
3) Event-bus:
That Handles the request between publishers and subscribers, and responsible for delivering of messages. The Event bus acts as an intermediary or black box that collects all the messages for publishers and forward to subscribers based on their interests.
Rails provide us Action Cable which is based on a pub-sub pattern.
Action Cable is a framework for working with web sockets. It allows real-time communication between server and client and it requires a constant connection between client and server.
In typical Http model. data is being exchanged between clients and the server. Each Time when client tries to communicate with the server, a new connection is required. Request data is wrapped in a large header which causes a delay in a request to be processed.
However, on the other hand, it is different in case of web sockets, Date is transferred between client and server based on a pre-established connection. it has less overhead as compared to HTTP model, which makes it faster to swap data.
Request header are transferred only one time at the start of the communication so the rest of the requests do not have to contain lengthy headers. Websockets are mostly categorized in two types of connections One of them is TCP and second is UDP, each of these have their own certain characteristics
Most of the applications do not require a real-time communication because with web sockets we have to response all the time, which can make things little complicated. However if you need a real-time communication in you application Action cable is a great option to consider.