<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AdeelSarim</title>
    <description>The latest articles on DEV Community by AdeelSarim (@adeelsarim).</description>
    <link>https://dev.to/adeelsarim</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F903383%2F7a3bee4b-98f7-470e-85ee-62cd09ce04be.png</url>
      <title>DEV Community: AdeelSarim</title>
      <link>https://dev.to/adeelsarim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adeelsarim"/>
    <language>en</language>
    <item>
      <title>Adding Ping tool in Rails App</title>
      <dc:creator>AdeelSarim</dc:creator>
      <pubDate>Thu, 04 Aug 2022 15:30:00 +0000</pubDate>
      <link>https://dev.to/adeelsarim/adding-ping-tool-in-rails-app-696</link>
      <guid>https://dev.to/adeelsarim/adding-ping-tool-in-rails-app-696</guid>
      <description>&lt;p&gt;You've heard of word ping or latency in network based applications. It's the time an application takes to respond to requests. For example, you're making a search on google. Your successful search will have following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In search bar, you enter some text&lt;/li&gt;
&lt;li&gt;Hit enter&lt;/li&gt;
&lt;li&gt;Request is being processed&lt;/li&gt;
&lt;li&gt;Response/results appear on webpage&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Latency is the time it takes from step-2 to step-4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;br&gt;
In this blog, we will learn how to add a ping/latency time tool to our rails app. After setting this tool up, we will be able to monitor ping time for our rails app. The basic idea is to see how fast/slow our app is working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
Here is a prerequisite before we start adding this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rails App running with webpacker (rails version should be &amp;gt;=5)&lt;/li&gt;
&lt;li&gt;Webpacker added&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s assume we have a rails app working with rails version =&amp;gt; 5 and webpacker is added. Here’s how we will add this ping tool:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1-Add Stimulus&lt;/strong&gt;&lt;br&gt;
Run this command to add stimulus&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Yarn add stimulus&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once it’s installed, use webpack to import it to the application. We will do this in &lt;code&gt;app/javascript/packs/application.js&lt;/code&gt;. In javascript directory, create controllers folder and then add &lt;code&gt;index.js&lt;/code&gt; there. Then reference this folder inside &lt;code&gt;application.js&lt;/code&gt;. This is how your &lt;code&gt;index.js&lt;/code&gt; will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"

const application = Application.start()
const context = require.context(".", true, /\.js$/)
application.load(definitionsFromContext(context))


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2- Add turbo-rails and redis&lt;/strong&gt;&lt;br&gt;
To setup &lt;code&gt;turbo-rails&lt;/code&gt;,  use following commands&lt;br&gt;
&lt;code&gt;bundle add turbo-rails&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails turbo:install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails turbo:install:redis&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we’re all set and ready to use it. Let’s generate a &lt;code&gt;PingsController&lt;/code&gt; with following command.&lt;br&gt;
&lt;code&gt;rails g controller ping&lt;/code&gt;&lt;br&gt;
Let’s add &lt;code&gt;#pong&lt;/code&gt; action which renders plain text response with &lt;code&gt;PONG&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PingController &amp;lt; ApplicationController
  def pong
    render status: :ok, body: "PONG"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s add a &lt;code&gt;/ping&lt;/code&gt; endpoint routed to the &lt;code&gt;#pong&lt;/code&gt; action like this:&lt;br&gt;
&lt;code&gt;get “/ping”, to: “ping#pong”, as: “ping”&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s create a form now in &lt;code&gt;app/views/shared/_ping.html.erb&lt;/code&gt; to hit this rout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div data-controller="ping" &amp;gt;
    &amp;lt;%= form_tag ping_path, method: :get, data: { action: "turbo:before-fetch-request-&amp;gt;ping#pauseRequest turbo:submit-end-&amp;gt;ping#measureLatency", "ping-target": "pingForm" } do %&amp;gt;
    &amp;lt;%= button_tag "Ping" %&amp;gt;
    &amp;lt;% end %&amp;gt;
    &amp;lt;span data-ping-target="latency"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s create a homepage and add this partial their so it would be usable from the homepage. First, let’s create &lt;code&gt;HomeController&lt;/code&gt; with &lt;code&gt;show&lt;/code&gt; action.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g controller home show&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now make this show action as root action in &lt;code&gt;route.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;root to: “home#show”&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now render the form partial on show page&lt;br&gt;
Add following code in &lt;code&gt;app/views/home/show.html.erb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;%= render "shared/ping" %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next step is to measure the time latency now.&lt;br&gt;
For this, we need to generate a stimulus controller.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g stimulus ping&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And then let’s define the getters and setters we need to serve our purpose. We have a &lt;code&gt;measureLatency()&lt;/code&gt; fucntion that will measure the latency. Then there's a &lt;code&gt;displayLatency()&lt;/code&gt; function that will display latency on page every second. All of this code will go in &lt;code&gt;app/javascript/controllers/ping_controller.js&lt;/code&gt; as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller } from "stimulus";
export default class extends Controller {
  static targets = ["pingForm", "latency"]

  pauseRequest(event) {
    event.preventDefault();

    setTimeout(() =&amp;gt; this.saveRequestTime());

    event.detail.resume();
  }

  saveRequestTime() {
    this.requestTime = new Date().getTime();
  }

  measureLatency() {

    this.saveResponseTime();

    this.latency = this.responseTime - this.requestTime;

    console.log(`${this.latency} ms`);
    this.displayLatency()

    setTimeout(() =&amp;gt; this.ping(), 1000)
  }

  displayLatency() {
    this.latencyTarget.textContent = this.latency + " ms"
  }


  saveResponseTime() {
    this.responseTime = new Date().getTime();
  }

  ping() {
    this.pingFormTarget.requestSubmit()
  }

  get requestTime() {
    return this._requestTime;
  }

  set requestTime(requestTime) {
    this._requestTime = requestTime;
  }

  get responseTime() {
    return this._responseTime;
  }

  set responseTime(responseTime) {
    this._responseTime = responseTime;
  }

  get latency() {
    return this._latency;
  }

  set latency(latency) {
    this._latency = latency;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we submit the form ping time will be printed after each second like in this picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KsM7kNbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qavg1jprukt3td43sdqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KsM7kNbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qavg1jprukt3td43sdqy.png" alt="Image description" width="790" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Discussion&lt;/strong&gt;&lt;br&gt;
This is how you can tell the end user about the latency time. It is more helpful when you're working to scale your app and improving the app performance. To make the UI better, you can print a graph as well.&lt;/p&gt;

&lt;p&gt;A simple Rails ping app is here in this &lt;a href="https://github.com/AdeelSarim/ping"&gt;Repo&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
