The Moral Dilemma of Monogamy

Marriages and committed monogamous relationships can deliver unmeasurable growth, support and joy to all those within and surrounding the couple. And while much of our social landscape is shifting…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Web Sockets

Have you ever used a messaging app that instantly shows a message without refreshing? Used a sports app and provide real-time updates on a close game? Or even used a website to collaborate with co-workers on a project real-time? How is this done, or even possible over the internet? With what we know about the internet, how can an app instantly show a message without a refresh? The internet, as we know it, has a request/response cycle. Your app, the client, makes a request to the server, and the server, in return, sends a response to your app. So with this information, it seems like we would have to programmatically have our app make a request over and over again, but making requests over and over again are not ideal for performance.

Basic HTTP Protocol request/response cycle

HTTP Long Polling

One of the most common ways to solve this problem has been with HTTP Long Polling. Since the client is always the one to initiate a transaction, the client would make an initial request, the server will then keep that request open until there is new information to be sent. The client, after it has relieved such information, will then request more information from the server, and the process repeats until the client stops the cycle. As you can imagine, HTTP Long Polling has it’s setbacks, such as being a lot more intense on the server.

Sockets

Let me introduce you to networking sockets. According to Oracle Java Documentation:

On a basic level, when we create a socket, we are creating a socket on the client side and the server side through ports. The server or client will listen on that specific port. For example, if we have a messaging app, we will send a request to the server, and create a socket dedicated to listening for the server. Whenever the server has new information to send, it will send it to that open port which is listening for the server.

This leads us to Web Sockets. Web sockets are built on top of the TCP layer, and is a standardized protocol for establishing two-way communication between a client and server.

According to MDN Web Docs, a web socket is as follows:

Pros

Cons

How do we use this in Rails? Let me introduce you to Action Cable and Event Machine.

Action Cable

The way Action Cable uses a Rack socket hijacking API. Action Cable then takes over control of the connections. Every time a user creates a connection with your Rails backend, Action Cable creates that socket and stores that connection in a Redis database. On the frontend there are JavaScript functions that are used to sync data between the Redis database and your frontend app.

The setup process is pretty long, but here is a blog that goes through a setup:

Event Machine

Unlike Action Cable, my understanding is the main difference is how and when new information is “pushed”. Event Machine will push information when a certain event occurs in the code, verse Action Cable will push when there is a change in the information.

Add a comment

Related posts:

Electronic music artists that do THIS are winning!

As with most genres of music; there is a massive amount of really amazing electronic producers/DJ’s out there, all over the world, that are struggling to get discovered and make a name and even a…

Establishing a Sales Team in New Markets

Companies often start their international expansion efforts by forming a local sales presence to drive revenue as quickly as possible. But rushing into this process can backfire unless you carefully…

5 Steps to Create a Workable Family Business Succession Plan

The importance of the family business to the United States economy continues to grow, and family businesses are beginning to reverse the trend of mega-businesses wiping mom-and-pop stores off the…