<?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: Johnson Ogwuru</title>
    <description>The latest articles on DEV Community by Johnson Ogwuru (@ogwurujohnson).</description>
    <link>https://dev.to/ogwurujohnson</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%2F45918%2F1caee5a7-9391-4900-ae00-31e6dd6dfb13.jpg</url>
      <title>DEV Community: Johnson Ogwuru</title>
      <link>https://dev.to/ogwurujohnson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ogwurujohnson"/>
    <language>en</language>
    <item>
      <title>Database Triggers in PostgreSQL</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Mon, 23 Nov 2020 19:10:57 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/database-triggers-in-postgresql-2097</link>
      <guid>https://dev.to/ogwurujohnson/database-triggers-in-postgresql-2097</guid>
      <description>&lt;p&gt;Ah, it's been a while I wrote about something. Been a busy year, so much to do, and so much learnt. &lt;/p&gt;

&lt;p&gt;In this article, I will try to explain what a database trigger is. When to use it, and a practical example of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition:
&lt;/h2&gt;

&lt;p&gt;A database trigger is a procedural code that is automatically executed in response to certain events on a particular table or view on a database.&lt;/p&gt;




&lt;h2&gt;
  
  
  Usage:
&lt;/h2&gt;

&lt;p&gt;Recently, I was working on a discord bot, at some point in the development processes, we needed a way to notify the discord users, of the status of their transaction. Because we had a &lt;code&gt;transactions&lt;/code&gt; table, with a status column, one way i could do this was to use a database trigger.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I am sure there are other solutions to this problem, but this was the solution I went with.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Setup:
&lt;/h2&gt;

&lt;p&gt;So I set up a trigger to listen for UPDATE events in the &lt;code&gt;transactions&lt;/code&gt; table. Here is the code required for doing this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="n"&gt;notify_transaction_status_change&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="k"&gt;trigger&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt;
      &lt;span class="k"&gt;DECLARE&lt;/span&gt;
      &lt;span class="k"&gt;BEGIN&lt;/span&gt;
        &lt;span class="n"&gt;PERFORM&lt;/span&gt; &lt;span class="n"&gt;pg_notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'transaction_status_changed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json_build_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s1"&gt;'operation'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TG_OP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'record'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row_to_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;text&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;RETURN&lt;/span&gt; &lt;span class="k"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="err"&gt;$$&lt;/span&gt; &lt;span class="k"&gt;LANGUAGE&lt;/span&gt; &lt;span class="s1"&gt;'plpgsql'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we are creating a database function, that would reference the trigger named, &lt;code&gt;transaction_status_changed&lt;/code&gt;. &lt;br&gt;
Below is the script for the trigger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt; &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TRIGGER&lt;/span&gt; &lt;span class="n"&gt;transaction_status_changed&lt;/span&gt; 
 &lt;span class="k"&gt;AFTER&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; 
 &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt; 
 &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;EACH&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; 
 &lt;span class="k"&gt;EXECUTE&lt;/span&gt; &lt;span class="k"&gt;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;notify_transaction_status_change&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trigger gets called after every update on the transactions table, for each row. So if you have 10 rows on the transactions table, if an update is performed on them, this trigger would run for each.&lt;/p&gt;

&lt;p&gt;Now the next thing to do would be to listen for this event triggers, get the payload from the event and do what whatever with it, in our case, we will be notifying the user who owns the transaction. &lt;/p&gt;




&lt;p&gt;We would need to create a connection to the database. With this connection created, the application would have to listen for events. &lt;br&gt;
Find the script I used in achieving this below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doConnectionSetupStuff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LISTEN transaction_status_changed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notification&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jsonResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//notify user when status of transaction is confirmed&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jsonResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;confirmed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;notifyUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jsonResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reconnectClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PostgresNotificationListener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;acquireRawConnection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Listening for events in Database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;doConnectionSetupStuff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reconnectClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;acquireRawConnection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;doConnectionSetupStuff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connected to DB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;PostgresNotificationListener&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the time of this writing, I was making use of [Knex], (&lt;a href="http://knexjs.org/"&gt;http://knexjs.org/&lt;/a&gt;), PostgreSQL and Typescript.&lt;/p&gt;

&lt;p&gt;The script creates a connection to the database, and when this connection ends, it reconnects the script to the database, so that it's constantly listening for trigger events.&lt;br&gt;
And when these events are received and certain conditions are met, the payload gets cleaned up and presented to a user or be used in making a logical decision within the application.&lt;/p&gt;

&lt;p&gt;There are plenty other use-cases for database triggers. One other use could be enforcing rules for certain columns in a table.&lt;/p&gt;

&lt;p&gt;** Quick note:&lt;br&gt;
if you are making use of Knex, here is how to setup the trigger and function in your table migration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;knex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;up&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create table&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transactions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increments&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;notNullable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sender&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Sender id (or null if type = withdrawal)&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;recipient&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Recipient id (or null if type = deposit)&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;withdraw_address&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;amount&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;notNullable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Transaction hash if type = deposit or withdrawal&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;status&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;defaultTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pending&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;created_at&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;useTz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;defaultTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;updated_at&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;useTz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;defaultTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Create function/trigger&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`
      CREATE FUNCTION notify_transaction_status_change() RETURNS trigger AS $$
      DECLARE
      BEGIN
        PERFORM pg_notify('transaction_status_changed', json_build_object(
            'operation', TG_OP,
            'record', row_to_json(NEW)
          )::text
        );

        RETURN NEW;
      END;
      $$ LANGUAGE 'plpgsql';
    `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Assign trigger&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
      CREATE TRIGGER transaction_status_changed 
      AFTER UPDATE 
      ON transactions 
      FOR EACH ROW 
      EXECUTE PROCEDURE notify_transaction_status_change()
  `&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;down&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DROP TRIGGER IF EXISTS transaction_status_changed ON transactions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DROP FUNCTION IF EXISTS notify_transaction_status_change CASCADE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dropTableIfExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transactions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Triggers are a powerful feature that can improve the efficiency of any application using a database. I hope someone finds it useful like I did. &lt;/p&gt;

&lt;p&gt;If you have any questions feel free to ask on the comment section or reach out to me on Twitter &lt;a href="https://twitter.com/devopsjay"&gt;@devopsjay&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;cover image from &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-relational-databases"&gt;https://www.digitalocean.com/community/tutorials/understanding-relational-databases&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tutorial</category>
      <category>postgres</category>
      <category>javascript</category>
      <category>database</category>
    </item>
    <item>
      <title>How To: Deploy Smart Contracts on the Energi Blockchain</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Tue, 23 Jun 2020 11:19:30 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/how-to-deploy-smart-contracts-on-the-energi-blockchain-1188</link>
      <guid>https://dev.to/ogwurujohnson/how-to-deploy-smart-contracts-on-the-energi-blockchain-1188</guid>
      <description>&lt;p&gt;For this tutorial, we would be making use of the following technologies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Solidity&lt;/li&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;Node JS&lt;/li&gt;
&lt;li&gt;Truffle&lt;/li&gt;
&lt;li&gt;Energi Blockchain&lt;/li&gt;
&lt;li&gt;Editor =&amp;gt; &lt;a href="https://code.visualstudio.com/Download"&gt;vscode&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Definitions&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://solidity.readthedocs.io/en/v0.6.10/"&gt;Solidity&lt;/a&gt;&lt;/strong&gt; is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.trufflesuite.com/"&gt;Truffle&lt;/a&gt;&lt;/strong&gt; is a world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.energi.world/"&gt;Energi Blockchain&lt;/a&gt;&lt;/strong&gt; Energi is a next-generation Proof of Stake (PoS) cryptocurrency that combines smart contract capabilities, decentralised governance, and a self-funding treasury.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;Installation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Follow the instruction found on the Energi wiki to download and install the Energi core Node on your device. I am using a mac, so i would make use of the mac installation documentation, found &lt;a href="https://docs.energi.software/en/advanced/core-node-mac"&gt;here&lt;/a&gt;, you can find download and installation instruction for other platforms like &lt;code&gt;linux&lt;/code&gt; and &lt;code&gt;windows&lt;/code&gt; in the documentation.&lt;/li&gt;
&lt;li&gt;Install Truffle globally by running this command in your terminal &lt;code&gt;npm install -g truffle&lt;/code&gt;. For mac users, if having permission issues, add &lt;code&gt;sudo&lt;/code&gt; before the command like this, &lt;code&gt;sudo npm install -g truffle&lt;/code&gt; and after inputing your password when prompted, truffle will be downloaded.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;vscode&lt;/code&gt; install the following &lt;a href="https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity"&gt;solidity&lt;/a&gt; extension, this extension helps with syntax highlighting, snippets and linting using &lt;code&gt;solhint&lt;/code&gt; when writing smart contracts with solidity.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Getting Started:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we would write a basic smart contract and deploy it on the energi test network.&lt;/p&gt;

&lt;p&gt;To get started, i would be creating an empty folder in my desktop named &lt;code&gt;energi-app&lt;/code&gt;. &lt;br&gt;
&lt;code&gt;mkdir energi-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this &lt;code&gt;cd&lt;/code&gt; into the folder we just created and create a truffle project by running the &lt;code&gt;init&lt;/code&gt; command.&lt;br&gt;
&lt;code&gt;cd energi-app&lt;/code&gt;&lt;br&gt;
&lt;code&gt;truffle init&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;johnson@Johnsons-MBP desktop % mkdir energi-app
johnson@Johnsons-MBP desktop % cd energi-app
johnson@Johnsons-MBP energi-app % truffle init

Starting unbox...
=================

✔ Preparing to download box
✔ Downloading
✔ cleaning up temporary files
✔ Setting up box

Unbox successful, sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

johnson@Johnsons-MBP energi-app %
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have our project setup, if you have done everything properly, when you open the &lt;code&gt;energi-app&lt;/code&gt; in vscode, the structure should look like what we have in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AaiuC4I4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lnh8oopf11fh3rh5qjxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AaiuC4I4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lnh8oopf11fh3rh5qjxy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you look at the project folder, you see a folder named &lt;code&gt;contracts&lt;/code&gt;, oh yeah🤓, that's where we would save our contracts.&lt;br&gt;
Now inside of the &lt;code&gt;contracts&lt;/code&gt; folder create a file &lt;code&gt;Hello.sol&lt;/code&gt;, inside of the file add the following solidity code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello There&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a very basic solidity smart contract code, this tutorial won't be focused on discussing solidity or smart contracts, to learn more visit the &lt;a href="https://solidity.readthedocs.io/"&gt;solidity&lt;/a&gt; documentation.&lt;/p&gt;

&lt;p&gt;To prepare our smart contract for deployment, we need to add one more file to our project, this is the file that would specify which contracts to deploy. Navigate to the &lt;code&gt;migrations&lt;/code&gt; folder and create a new file &lt;code&gt;2_migrate_hello.js&lt;/code&gt;.&lt;br&gt;
The numbering on the file is important, it's how truffle knows which contracts to deploy when. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that for every independent contract you create, you would need to create a migration file for. But for contracts dependent on each other, you can batch their migration together in the same migration file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add the following code in the &lt;code&gt;2_migrate_hello.js&lt;/code&gt; migration file created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;artifacts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Hello.sol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deployer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;deployer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to deploy our contract on the Energi test-network, you need to follow the steps below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Start Energi node, but this time make use of this command if you are on a mac&lt;br&gt;
&lt;code&gt;$HOME/energi3/bin/energi3 --testnet --rpc&lt;/code&gt;.&lt;br&gt;
This would start an rpc enabled testnet, using the Energi core node app you downloaded. &lt;br&gt;
The RPC link exposed by default would be &lt;code&gt;127.0.0.1:49796&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;code&gt;truffle-config.js&lt;/code&gt; file, and replace the code there with the one attached below:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HDWalletProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@truffle/hdwallet-provider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// const mnemonic = fs.readFileSync(".secret").toString().trim();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;privateKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0x1...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;energiTestnet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;HDWalletProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;privateKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`http://127.0.0.1:49796`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;network_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;49797&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    
      &lt;span class="na"&gt;gas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5500000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;timeoutBlocks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// # of blocks before a deployment times out  (minimum/default: 50)&lt;/span&gt;
      &lt;span class="na"&gt;skipDryRun&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;     &lt;span class="c1"&gt;// Skip dry run before migrations? (default: false for public nets )&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Edit the &lt;code&gt;from&lt;/code&gt; property in the network object to the address you want to deploy the contract with, and also change the private key value to the private key of the account you use in the &lt;code&gt;from&lt;/code&gt; property.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Also make sure that the account you use for this, is being controlled by the node you started initially, and it has some funds, 3tnrg(test nrg) won't be bad. You can request some tnrg from the Energi team on &lt;a href="https://discord.com/invite/sCtgNC3"&gt;Discord&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;Note, in the truffle config file, you can either use a private key or mnemonic phrase, whichever works for you, but don't be like me, make sure you store this information securely, you can use a .env file, and make sure you don't commit it to a public repository.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Install the &lt;code&gt;hdwallet-provider&lt;/code&gt; by running the following command&lt;br&gt;
&lt;code&gt;yarn init&lt;/code&gt;&lt;br&gt;
&lt;code&gt;yarn add @truffle/hdwallet-provider&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then run the following command to deploy your contract, make sure you are in your project directory.&lt;br&gt;
&lt;code&gt;truffle deploy --network=energiTestnet&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If all goes well, you should be able to see this output in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;johnson@Johnsons-MBP energi-app % truffle deploy --reset --network=energiTestnet

Compiling your contracts...
===========================
&amp;gt; Everything is up to date, there is nothing to compile.



Starting migrations...
======================
&amp;gt; Network name:    'energiTestnet'
&amp;gt; Network id:      49797
&amp;gt; Block gas limit: 40000000 (0x2625a00)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   &amp;gt; transaction hash:    0xc88f6525e15caf806f755b16f682a8f7c7a37f91051f833f766410da0a4bdbb2
   &amp;gt; Blocks: 1            Seconds: 24
   &amp;gt; contract address:    0xa16FC529F5C493c12031657BF93A07e9DBfB6538
   &amp;gt; block number:        194105
   &amp;gt; block timestamp:     1592907159
   &amp;gt; account:           0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A
   &amp;gt; balance:             3631.499979394
   &amp;gt; gas used:            188483 (0x2e043)
   &amp;gt; gas price:           20 gwei
   &amp;gt; value sent:          0 ETH
   &amp;gt; total cost:          0.00376966 ETH


   &amp;gt; Saving migration to chain.
   &amp;gt; Saving artifacts
   -------------------------------------
   &amp;gt; Total cost:          0.00376966 ETH


2_migrate_hello.js
==================

   Replacing 'Hello'
   -----------------
   &amp;gt; transaction hash:    0xa9f76b3df7a191d56d769655428916f8c0128ecdd026bf6af26ef3a1b9403127
   &amp;gt; Blocks: 0            Seconds: 76
   &amp;gt; contract address:    0xA309F47d94768c2357780E8b55dC77981494a995
   &amp;gt; block number:        194107
   &amp;gt; block timestamp:     1592907306
   &amp;gt; account:             0xa57ed899Cd9587952Cbf284c9459DB4fF6DEe53A
   &amp;gt; balance:             3631.496548394
   &amp;gt; gas used:            129549 (0x1fa0d)
   &amp;gt; gas price:           20 gwei
   &amp;gt; value sent:          0 ETH
   &amp;gt; total cost:          0.00259098 ETH


   &amp;gt; Saving migration to chain.
   &amp;gt; Saving artifacts
   -------------------------------------
   &amp;gt; Total cost:          0.00259098 ETH


Summary
=======
&amp;gt; Total deployments:   2
&amp;gt; Final cost:          0.00636064 ETH


johnson@Johnsons-MBP energi-app % 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Because i have compiled this before, it won't display the compilation message, but would rather say it's been compiled before, but on yours, you would be able to watch it compile before migration starts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lastly, let's check our deployed smart contract on the Energi test-network block-explorer. From my deploy log, my hello smart contract address is &lt;code&gt;0xA309F47d94768c2357780E8b55dC77981494a995&lt;/code&gt;. When you get to the Energi test-network &lt;a href="https://explorer.test3.energi.network/"&gt;block explorer&lt;/a&gt;, search for the smart contract address. For me this is what i see, my smart contract deployed, and chilling on the Energi test-network. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---6JXoGlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mgj2dy0c1r6chaoev23e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---6JXoGlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mgj2dy0c1r6chaoev23e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Finally, to deploy to the main net, all we have to do is remove the testnet we used when starting the node and start the node with this command instead.&lt;br&gt;
&lt;code&gt;$HOME/energi3/bin/energi3 --rpc&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article we have covered the basics of deploying on Energi, with truffle. For further readings and information about the Energi Blockchain check out the documentation, found &lt;a href="https://docs.energi.software/en/home"&gt;here&lt;/a&gt;. Reach out to me also if you have any questions here or on Twitter.&lt;/p&gt;

&lt;p&gt;You can find the repository for the project &lt;a href="https://github.com/ogwurujohnson/deploy_to_energi"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>blockchain</category>
      <category>energi</category>
    </item>
    <item>
      <title>2019 - The Path to Growth</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Tue, 31 Dec 2019 20:57:37 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/2019-the-path-to-growth-44ch</link>
      <guid>https://dev.to/ogwurujohnson/2019-the-path-to-growth-44ch</guid>
      <description>&lt;p&gt;Hey, Johnson from the past, I got your letter about traveling abroad. Lol. I’m sorry to disappoint you man, you are still in Nigeria. By the way, this is Johnson from the future. But like you already know, I’m no bringer of bad news alone, so I’m pretty much going to tell you, You had a bang of a year in 2019. From getting a job to losing it and getting another. &lt;br&gt;
Well Let’s start and entertain our readers gradually, shall we?&lt;/p&gt;




&lt;h4&gt;
  
  
  Making sacrifices for Growth
&lt;/h4&gt;

&lt;p&gt;2019 started with me having a strong disagreement with my parents concerning a career decision I was about making, a decision that led to a lot of &lt;code&gt;ehhhhhh&lt;/code&gt;🥴 and &lt;code&gt;ahaa&lt;/code&gt;🤯 moments in my life. So the thing is, I was working in a company where I was sort of like the &lt;code&gt;star developer&lt;/code&gt; and for me, that didn’t feel right, I knew I wasn’t growing, but I was earning good money, something some dev's in Nigeria would be comfortable with. But you know it isn’t just about the money most times in tech, rather is what you know and how competent you are at defending it. That’s what gets you the next best gig. So I set plans in motion, I was going to work for a Nigerian company, that has lots of devs and a good track record of producing great Engineers and by February of 2019, I left my previous company and joined Andela by April 2019. &lt;/p&gt;

&lt;p&gt;Joining Andela for me then meant accepting a compensation 30% of what I used to earn and it also meant I had to either pause on the side gigs I had or let them go, most of which I let go of &lt;code&gt;(this wasn’t Andela’s doing, I decided on this because I needed less distraction at the time)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5Fpt3ypj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/u5cw1bxt2okja71xjoo5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5Fpt3ypj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/u5cw1bxt2okja71xjoo5.gif" alt="Alt Text" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yeah, I know that was crazy, but to me then what mattered was the growth coming my way and the relationships I was going to build working with amazing Engineers. Well, it all worked out well. &lt;br&gt;
For a start I got enrolled at Lambda School with all expenses paid for by Andela. Joining Lamba School opened me up to a world of crazy possibilities and networks, I can comfortably boast of having friends around the world right now from India to Europe. That move was what got me something I never had in the bags for 2019, but it happened. Well, it didn’t all work out as I planned.&lt;/p&gt;




&lt;h4&gt;
  
  
  The hard times
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Losing my job
&lt;/h5&gt;

&lt;p&gt;By September 2019, I got the shocker of the decade, I lost my job due to some complicated issues, my 6months old Job, lol. The same Job I had sacrificed my well-paying Job for, not because I wasn't technical enough. The reason can be found &lt;a href="https://andela.com/insights/the-future-of-andela/"&gt;here&lt;/a&gt;.&lt;br&gt;
I was bitter for the best part of 2 weeks. But during that time I got to realize that I got what I came for, the growth, the relationships, the experience working in a fast-paced environment and that was when I came out of my bitterness. &lt;/p&gt;

&lt;h5&gt;
  
  
  House rent and Upkeep sets in
&lt;/h5&gt;

&lt;p&gt;Just then I got hit with another realization, my rent was expiring the same month. was a little depressed, no I was depressed!!!!! 😂, but fortunately, we got paid our severance package, combined with my savings I paid off my rent and had some money left for upkeep.  I realized I was going to be in serious financial crisis if I wasn’t careful, the least I could stay without a job would say 4 or 5months. So I set out to find another job.&lt;/p&gt;

&lt;h5&gt;
  
  
  Several interviews and failings...
&lt;/h5&gt;

&lt;p&gt;I set plans on how to move on, prepared 5 variations of my Resume and 25 different cover letters, all ready for some serious, high-level Job application stuff.&lt;br&gt;&lt;br&gt;
I have a bookmark of companies hiring remotely and others requiring relocation I had started compiling since 2018, so I kicked it open and started applying to each of them, I was applying to 10-20 Jobs every day except for Saturdays and Sundays, I felt the recruiters needed some rest away from work. &lt;br&gt;
At the time, rejections were dropping like 2 or 3 a day, It was tough, but I got used to it. Funny thing is, I didn’t just get used to being rejected, I got used to interviewing, it became more fun than boring. I felt I was learning a lot so I kept on applying for any Job that seemed fun and challenging. In some days I’m sure I applied for 30 jobs or more, it was fun, the rejections were cool, the interviews too, I even interviewed for a managerial position I was sure I wasn’t qualified for just to know what it feels like. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eC9GQjny--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/rghc2e0f3ao8t29jw1nz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eC9GQjny--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/rghc2e0f3ao8t29jw1nz.gif" alt="Alt Text" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Funny thing is, two of the rejections I got were because the companies chose to go with a different candidate they felt was asking for something lower than what they were offering me, not even what I was asking, crazy right? &lt;br&gt;
After the rigorous application process and technical interviews, the first one had me go through. They asked for a rate which I gave them, they gave me their offer, which wasn’t nice, but was something I could accept, so I opted to think about and accepted, only for me to be slammed with a rejection mail the week I was supposed to receive an offer from them, stating that the opted for another candidate whose rate was lower than the offer they wanted to give me. &lt;br&gt;
The second company accepted me after the interviews, we didn’t discuss price since they already had a price labeled on their career page and I was cool with was on the page, it was cool money mehn, I was happy that I was going to blow, lol😂. Then it dawned on me when I got an email from them the day I was supposed to resume, that they won’t go ahead with me because, well you can guess. &lt;br&gt;
It was meant to be a 9 months contract and I lost it also because they didn’t want to pay what they advertised again and because they found a cheaper labor.&lt;br&gt;
Amidst all of this, I was still hitting it hard;&lt;/p&gt;




&lt;h4&gt;
  
  
  Achievements 0f 2019
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;I got to attend 2 conferences and volunteered In one, which was part of my goals for 2019&lt;/li&gt;
&lt;li&gt;I completed the Hacktoberfest challenge for the first time, contributing to open source projects&lt;/li&gt;
&lt;li&gt;My articles reached over 100k readers and had over 4k interacting with them&lt;/li&gt;
&lt;li&gt;I wrote another 15 articles this year, some of which were accepted by the community, my best being&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/ogwurujohnson/demystifying-the-javascript-call-stack-1ppc"&gt;Demystifying the JavaScript call stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/ogwurujohnson/optimizing-react-components-with-error-boundaries-2doh"&gt;Optimizing React Components with Error Boundaries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/ogwurujohnson/introduction-to-higher-order-components-hocs-in-react-273f"&gt;Introduction to Higher-Order Components(HOCs)In React&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can find the rest &lt;a href="https://dev.to/ogwurujohnson"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smashed through 3k contributions on Github and had a streak of 322 days of active contributions on Github&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3VZmp_JM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/rzpg1p5c79rtvkpsic0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3VZmp_JM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/rzpg1p5c79rtvkpsic0u.png" alt="Alt Text" width="880" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Got into Lambda School which was so fun and eye-opening&lt;/li&gt;
&lt;li&gt;I started getting paid to write about certain technologies, and I’m currently writing for 3 companies so far. These weren’t included in the 15 articles I talked about.&lt;/li&gt;
&lt;li&gt;Lastly, my 3 weeks of Job hunting paid off, by the end of October 2019, I got an amazing offer from a company In the BVI(British Virgin Islands), and by November 2019, I resumed as a Remote Software Engineer, after submitting over 200 applications and 50 rejections. Something I didn't think was going to happen in 2019. 
It has been an amazing experience so far, I have grown more attached to my bedroom 🤪 and laptop. And yes I still miss interviewing. &lt;/li&gt;
&lt;li&gt;I drew closer to God as well, because I had more time studying and reading about him and his will.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Appreciations
&lt;/h4&gt;

&lt;p&gt;2019 won’t have been fun completely without appreciating  a few of these people,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Gift Egwenu and Paradise Kelechi, these two were instrumental in my getting into Andela, they prepared me for what was expected, interview preparations and through the recruitment phase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Everyone I met at Andela, awesome folks, you guys are amazing, our early morning gist and evening arguments before the close of work. It was fun working with you guys. &lt;a href="https://twitter.com/easybuoy"&gt;Ezekiel&lt;/a&gt;, &lt;a href="https://twitter.com/rexben001"&gt;Benjamin&lt;/a&gt;, &lt;a href="https://twitter.com/AwaMelvine"&gt;Melvine&lt;/a&gt;, &lt;a href="https://twitter.com/nobleobioma_"&gt;Noble&lt;/a&gt;, Kelechi, &lt;a href="https://twitter.com/wanderluster_xo"&gt;Nabeelah&lt;/a&gt;, &lt;a href="https://twitter.com/iam_nedsoft"&gt;Chinedu&lt;/a&gt;, &lt;a href="https://twitter.com/timileyinojo"&gt;Timi&lt;/a&gt;, &lt;a href="https://twitter.com/Haywhyze"&gt;Yusuf&lt;/a&gt;, Kunle, Yemi, Pascal, &lt;a href="https://twitter.com/basil_cea"&gt;Basil&lt;/a&gt;, Anu, &lt;a href="https://twitter.com/_criztus"&gt;Vincent&lt;/a&gt;, James, Wasiu.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/sarah_chima"&gt;Sarah Chima&lt;/a&gt; has proved to be a friend in troubled times, she is one of the people I can comfortably say I look up to. And I know has my back any day. Though we have some shared goals we didn’t complete lol 😂.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Benjamin Grabow, Matt Hardman, Anna Winther, amazing folks I’m proud to call friends now.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/virsamarvir"&gt;Samar Vir&lt;/a&gt;, Dude legit sent me poems when I lost my job, made sure I was mentally stable at all times and has been an amazing friend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/Jibsonbade"&gt;Steve&lt;/a&gt; and Tomi, these are my best friends any day, they have always been there for me.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/sarah_edo"&gt;Sarah Drasner&lt;/a&gt; and &lt;a href="https://twitter.com/techgirl1908"&gt;Angie&lt;/a&gt;, thanks for the motivations and encouragements, whenever I reach out to you guys.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ogay, one person that has always been there for me, the good and the bad times, I appreciate you alot my paddy. Gelic 😛&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Things I'm looking forward to in 2020&lt;/p&gt;

&lt;p&gt;Next year, I’m looking forward to doing a lot of stuff;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traveling the world, but I’m starting from Africa though.&lt;/li&gt;
&lt;li&gt;I planned on giving a talk this year but failed on this because I didn’t submit much CFP’s. So I’m pushing it to 2020.&lt;/li&gt;
&lt;li&gt;I plan on becoming a mobile Engineer by the end of 2020, right now it's just a passion I want to explore.&lt;/li&gt;
&lt;li&gt;Create some contents on YouTube and write more articles.&lt;/li&gt;
&lt;li&gt;I never got to publish the book I am working on, but I hope to do by 2020.&lt;/li&gt;
&lt;li&gt;Draw even closer to God&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In all, it has been an amazing year, and I’m grateful for everything and everyone in my life.&lt;/p&gt;







&lt;p&gt;Yo Dude, Future Johnson, when you see this piece, remember to live not just the good life alone, but a more balanced one. I look forward to hearing from you in 2020.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>yearinreview</category>
      <category>career</category>
      <category>growth</category>
    </item>
    <item>
      <title>Implementing a realtime geo-location tracker with VueJS and Ably</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Wed, 27 Nov 2019 16:07:04 +0000</pubDate>
      <link>https://dev.to/ably/implementing-a-real-time-location-tracker-with-vuejs-and-ably-3523</link>
      <guid>https://dev.to/ably/implementing-a-real-time-location-tracker-with-vuejs-and-ably-3523</guid>
      <description>&lt;p&gt;In this tutorial, we will see how to use the &lt;a href="https://www.ably.io/documentation/realtime" rel="noopener noreferrer"&gt;Ably Realtime client library&lt;/a&gt; to build a realtime location Tracking PWA with Vue js. Ably enables realtime data sharing using Pub/Sub messaging architecture via a concept called &lt;a href="https://www.ably.io/channels" rel="noopener noreferrer"&gt;channels&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the purpose of this tutorial, we'll be discussing &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to build PWAs in Vue&lt;/li&gt;
&lt;li&gt;How to use the Geolocation in APIs in PWAs&lt;/li&gt;
&lt;li&gt;How to share the live location updates with other clients using Ably&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The application we would be building will make use of Ably to share realtime location data in the app. As an example, we would be building a friend location tracker with the following specifications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any user who comes online on the app is able to see their location represented by a blue pin.&lt;/li&gt;
&lt;li&gt;The location of any other users that are online needs to be represented by a red pin&lt;/li&gt;
&lt;li&gt;All these location pins need to be plotted on a map and need to move in real-time as the user device moves.
Here is a link to a &lt;a href="https://friend-finders.netlify.com" rel="noopener noreferrer"&gt;demo&lt;/a&gt; of the application we will build.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And a screenshot of it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbsyeasesyaq3p3xcbgpn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbsyeasesyaq3p3xcbgpn.png" alt="landing page"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1bzdqhs232zob7krqx30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1bzdqhs232zob7krqx30.png" alt="application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Vue.js is an open-source model–view–viewmodel(MVVM) JavaScript framework for building user interfaces and single-page applications. It was created by Evan You and is maintained by him and the rest of the active core team members coming from various companies such as Netlify and Netguru.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.smashingmagazine.com/2016/08/a-beginners-guide-to-progressive-web-apps/" rel="noopener noreferrer"&gt;Smashing Magazine&lt;/a&gt;, defined a PWA as a progressive web application that takes advantage of the latest technologies to combine the best of web and mobile apps. We could think of it as an application built with web technologies but behaves like a mobile application.&lt;/p&gt;

&lt;p&gt;Once a site has a PWA built and ready to go, Chrome will push it to be installed on a user’s mobile device so long as it meets the following criteria:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It is running under HTTPS - Emphasis on the “S” there. Your site must be secured with an SSL certificate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It has a Web App Manifest - This is a JSON file that lets you customize various features of your app such as name, colors, design, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It has a Service Worker - This is a JavaScript file that allows your PWA to work offline (to the extent that it is capable, of course). It’s essentially the script that is always working tirelessly in the background.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Step1 - Setup an Ably Account&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to run this tutorial locally, you will need an Ably API key. If you are not already signed up, you should sign up now for a free Ably account. Once you have an Ably account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log into your app dashboard&lt;/li&gt;
&lt;li&gt;Under “Your apps”, click on “Manage app” for any app you wish to use for this tutorial, or create a new one with the “Create New App” button
Click on the “API Keys” tab&lt;/li&gt;
&lt;li&gt;Copy the secret “API Key” value from your Root key and store it so that you can use it later in this tutorial&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fotjyo0hyopkylm2uv571.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fotjyo0hyopkylm2uv571.png" alt="setup ably account"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step2: Vue CLI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make sure you have node and npm installed. If you do, we would need to install Vue CLI, which is a boilerplate that speeds up the process of getting started with building a vue application. &lt;/p&gt;

&lt;p&gt;We start off by creating the basic setup and the file structure of our app. To speed things up, we will bootstrap the app with vue-cli. &lt;br&gt;
First, we need to install the vue CLI tool globally.&lt;br&gt;
&lt;code&gt;yarn global add @vue/cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we can instantiate the template by&lt;br&gt;
&lt;code&gt;vue init pwa friend-finder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will be prompted to pick a preset — I recommend the following configuration with your personal details where necessary of course:&lt;br&gt;
&lt;code&gt;? Project name friend-finder&lt;br&gt;
 ? Project short name: fewer than 12 characters to not be truncated on home screens (default: same as name) friend-finder&lt;br&gt;
 ? Project description A simple friend finder &lt;br&gt;
 ? Author Johnson Ogwuru &amp;lt;ogwurujohnson@gmail.com&amp;gt;&lt;br&gt;
 ? Vue build runtime&lt;br&gt;
 ? Install vue-router? No&lt;br&gt;
 ? Use ESLint to lint your code? Yes&lt;br&gt;
 ? Pick an ESLint preset Standard&lt;br&gt;
 ? Setup unit tests with Karma + Mocha? No&lt;br&gt;
 ? Setup e2e tests with Nightwatch? No&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the Vue build configuration, we can choose the smaller runtime option.&lt;/p&gt;

&lt;p&gt;Next, run &lt;code&gt;yarn&lt;/code&gt; or &lt;code&gt;npm install&lt;/code&gt; to install all the dependencies. To start the development mode just run &lt;code&gt;yarn start&lt;/code&gt; or &lt;code&gt;npm start&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don’t get confused by all the files in the project — For now, we won’t touch most of them. If you want to learn more about the template’s structure check out the documentation. I can only recommend it!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we would need to install all the packages we would be using in this project &lt;code&gt;yarn add ably vue2-google-maps&lt;/code&gt; or &lt;code&gt;npm install ably vue2-google-maps&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After installing the packages, head straight to &lt;a href="https://support.google.com/googleapi/answer/6158862?hl=en" rel="noopener noreferrer"&gt;https://support.google.com/googleapi/answer/6158862?hl=en&lt;/a&gt;, to create a project and then get an API key for making requests to Google Cloud server. This API key we get from Google is what we need to be able to make requests to the Google Maps API. Without it, we won't have the authentication needed to make a request to the Google Maps API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step3: Building Our Product&lt;/strong&gt;&lt;br&gt;
By running this script &lt;code&gt;vue init pwa friend-finder&lt;/code&gt; on the terminal to bootstrap our application we had already instructed vue to create a PWA application for us. And now for the application, we are building.&lt;/p&gt;

&lt;p&gt;We will start with getting our map displayed on the application and to do this we would make use of the npm package &lt;code&gt;vue2-google-maps&lt;/code&gt;. Since we already have it installed we would start making use of it.&lt;/p&gt;

&lt;p&gt;We would also need to install the Vue router, our application would require an onboarding page detailing how to use the application. So add router to our vue application, run &lt;code&gt;yarn add vue-router&lt;/code&gt; on the terminal.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.1 - *&lt;/em&gt; Navigate to the &lt;code&gt;main.js&lt;/code&gt; file, which is located in the &lt;code&gt;src&lt;/code&gt; folder and paste the following code, replacing what we had in the file initially.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;


      &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./router&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

      &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;VueGoogleMaps&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue2-google-maps&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

      &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;VueGoogleMaps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;load&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;your google map key&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;libraries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;places&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;

      &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productionTip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

      &lt;span class="cm"&gt;/* eslint-disable no-new */&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;App/&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;components&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In the above code, we get to import the google maps library and instantiate it, while providing the necessary credentials like your generated API key. Then we instantiate the Vue class, passing to it our template, router and component of choice which is App.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.1.1 - *&lt;/em&gt; Next up, you need to create the &lt;code&gt;components/Application.vue&lt;/code&gt; file and replace the code in it with this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

  &lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;GmapMap&lt;/span&gt;
        &lt;span class="na"&gt;:center=&lt;/span&gt;&lt;span class="s"&gt;"{lat: 10, lng:10}"&lt;/span&gt;
        &lt;span class="na"&gt;:zoom=&lt;/span&gt;&lt;span class="s"&gt;"15"&lt;/span&gt;
        &lt;span class="na"&gt;map-type-id=&lt;/span&gt;&lt;span class="s"&gt;"terrain"&lt;/span&gt;
        &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 100%; height: 100%"&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/GmapMap&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;In the code above, we create our map using the GmapMap component and pass it the following properties &lt;code&gt;zoom, center, map-type, style&lt;/code&gt; which contributes to how the map looks on the browser.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.2 - *&lt;/em&gt; The next thing in our bucket list would be to have our application retrieve the user's location and to do this we would be making use of the geolocation API available in HTML5. Paste the following code inside the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag in &lt;code&gt;app.vue&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

   &lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;geolocation&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errorStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Geolocation is not available.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gettingLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;geolocation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;watchPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gettingLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latitude&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lng&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longitude&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;latitude&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;lng&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longitude&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usersName&lt;/span&gt;
          &lt;span class="p"&gt;};&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userlocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateRoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gettingLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errorStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;In the code above we are fetching the users location, which we wrap in an object together with the users' name (which we would provide how it would be supplied later on), then we call a method that handles publishing to &lt;code&gt;ably realtime&lt;/code&gt; with the users' credential as an argument. The &lt;code&gt;methods&lt;/code&gt; property in the file, is how vue s specifies methods to be used in the application. They are functions that hang off of an object-typically the Vue instance itself or a Vue component.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.2.1 - *&lt;/em&gt; Next we would create the method &lt;code&gt;updateRoom&lt;/code&gt;, which we would use to update the presence of a user in a channel, while at the same time sending certain information about the users' current location.&lt;br&gt;
Before we do that, we would want to import ably and set it up, so on the lines following the opening &lt;code&gt;script&lt;/code&gt; tag, paste the following code&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Ably&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ably&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ably&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Ably&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Realtime&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
     &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;your ably key&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now we have imported the &lt;code&gt;ably&lt;/code&gt; library and configured ably to use in our application. In order for a user to be present in the channel, the user must be identified by having a &lt;code&gt;clientId&lt;/code&gt;. A single &lt;code&gt;clientId&lt;/code&gt; may be present multiple times on the same channel via different client connections. As far as Ably is concerned, these are different members of the presence set for the channel, however, they will be differentiated by their unique &lt;code&gt;connectionId&lt;/code&gt;. For example, if a client with ID “Sarah” is connected to a chat channel on both a desktop and a mobile device simultaneously, “Sarah” will be present twice in the presence member set with the same client ID, yet will have two unique connection IDs. A member of the presence set is therefore unique by the combination of the &lt;code&gt;clientId&lt;/code&gt; and &lt;code&gt;connectionId&lt;/code&gt; strings.&lt;/p&gt;

&lt;p&gt;So it's time for us to send location data to ably and update data too by making use of the &lt;code&gt;updateRoom&lt;/code&gt; method. Copy the following code and paste under the &lt;code&gt;fetchData&lt;/code&gt; method.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

  &lt;span class="nf"&gt;updateRoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error updating presence data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We have successfully updated our data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In the above code, we update the information of the user, in their registered ably channel, and this makes it possible for everyone subscribed to the channel to receive the new update in real-time without page refreshes, leveraging the power of web sockets&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.3 - *&lt;/em&gt; Next we need a way to listen to changes in the channel so that when a user's presence is updated, all users in the channel get notified. And to do these we would have to add some extra block of codes in the &lt;code&gt;created()&lt;/code&gt; method of vue js. The &lt;code&gt;created()&lt;/code&gt; method in vue is a method that allows you to add code, once the vue instance is created. So now we would be saying once the vue instance is created keep checking if an update exists and subscribe to the channel so that any information update on the channel the user can get it. So above the &lt;code&gt;methods()&lt;/code&gt; block at this piece of code. But before that, we need to get some information from the user, like their names and the name of the channel they would love to join. Type the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;mounted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;To get started, input your name in the field below and locate your friends around based on your location, please turn on your location setting &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt; What is your name?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usersName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enter the name of the channel you are interested in&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channelName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt; 


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmhqgjvexraaa6xwq0mn1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmhqgjvexraaa6xwq0mn1.png" alt="Ask for users name"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd1oioud7clznycsmh5ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fd1oioud7clznycsmh5ab.png" alt="Ask for users choice of channel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, we add the prompt code in the mounted() property, every code inside this property run immediately the component mounts. So we pick this information and store them in the assigned state variables.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;created&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ably&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channelName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error attaching to the channel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We are now attached to the channel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userlocation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error entering presence&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We are now successfully present&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;update&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;presenceMsg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;presenceMsg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Received a &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;presenceMsg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; from &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;presenceMsg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;markers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userlocation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://maps.google.com/mapfiles/ms/icons/blue-dot.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://maps.google.com/mapfiles/ms/icons/red-dot.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onlineUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;There are now &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; clients present on this channel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;In the above code, after subscribing to the channel, we expect ably to update us in real-time of changes in the channel, which include users presence activities on the channel, which gets stored in a variable, so what we do is for every 3 seconds, pick whatever is in the document and add to a slice of state to keep our application up to date, in response to the real-time data supplied by ably.&lt;/p&gt;

&lt;p&gt;We needed each user on the map to have different colors, in that we wanted the owner of the device having a different marker color from the rest of the other markers on the map, which was why we added this logic to the code above  &lt;code&gt;return {...mem.data, icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So what happens is that, in this line &lt;code&gt;var channel = ably.channels.get(this.channelName);&lt;/code&gt; we are either creating a new channel or joining a channel if the channel exists. So if the channel you submitted when the component mount exists, all you do is join it, but if it doesn’t then ably creates as a new one and you are able to ask others to join too. Now because we need to pick certain information from users when they join the channel, we would have the application send their location data when it’s registering their presence in the channel by using the line of code starting with &lt;code&gt;channel.presence.enter...&lt;/code&gt;, and also while the user is in the channel, they want to be kept updated as regards events in the channel, so to listen for events, we would use one of Ably’s API, the &lt;code&gt;channel.presence.subscribe...&lt;/code&gt;. Here you specify the event you are listening for, in our case it is &lt;code&gt;update&lt;/code&gt;, so when it is fired, we want to get the information of everyone in the channel, with their location data, Ably’s  &lt;code&gt;channel.presence.get&lt;/code&gt; API gets us this information.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3.4 - *&lt;/em&gt; Next stop we need a set of state slices, some of which we have already used in the codes above and I'm certain you already started asking yourself where the came from, well here they are. Add the following code on top of the &lt;code&gt;methods()&lt;/code&gt; block.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

   &lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;usersName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;gettingLocation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;initialPosition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;lng&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;zoom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;markers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;userlocation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftuafgzmcwf1ry01s6pj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftuafgzmcwf1ry01s6pj8.png" alt="Complete application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In conclusion:&lt;/strong&gt; The information contained here might be quite overwhelming, so for that, I have provided the repo for the project for further understanding. You can find the demo &lt;a href="https://friend-finders.netlify.com/#/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ably/tutorials/tree/realtime-location-tracking-vuejs" rel="noopener noreferrer"&gt;GitHub Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>showdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Favorite Book Thread</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Fri, 04 Oct 2019 15:26:59 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/favorite-book-thread-28h2</link>
      <guid>https://dev.to/ogwurujohnson/favorite-book-thread-28h2</guid>
      <description>&lt;p&gt;Watching my friend's vocabulary improve tremendously, after he read 2 books in 3months, have pushed me to want to up my reading game. Suggestions of books I should start reading would be appreciated.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Demystifying the JavaScript call stack</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Fri, 13 Sep 2019 15:11:53 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/demystifying-the-javascript-call-stack-1ppc</link>
      <guid>https://dev.to/ogwurujohnson/demystifying-the-javascript-call-stack-1ppc</guid>
      <description>&lt;p&gt;JavaScript is a single-threaded, single concurrent language, meaning it can handle one task at a time or a piece of code at a time. It has a single call stack, which along with other parts constitutes the Javascript Concurrency Model (implemented inside of V8). &lt;/p&gt;

&lt;p&gt;This article would be focusing on explaining what the call stack is, and why it's important and needed by JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;At the most basic level, the call stack is a data structure that utilizes the Last in, First out(LIFO) principle to store and manage function invocations.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since the call stack is single, function execution is done one at a time from top to bottom, making the call stack synchronous. In managing and storing function invocations the call stack follows the Last in, First Out principle(LIFO) and this entails that the last function execution that gets pushed into the call stack is always the one to be cleared off, the moment the call stack is popped.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;What purpose does the call stack serve in a JavaScript application? How does JavaScript make use of this feature?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When the JavaScript engine runs your code an execution context is created, this execution context is the first execution context that is created and it is called the &lt;code&gt;Global Execution Context&lt;/code&gt;. Initially, this Execution Context will consist of two things - a global object and a variable called &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now when a function is executed in JavaScript (when a function is called with the &lt;code&gt;()&lt;/code&gt; after its label), JavaScript creates a new execution context called the &lt;code&gt;local execution context&lt;/code&gt;. So for each function execution, a new execution context is created&lt;/p&gt;

&lt;p&gt;Just in case you were wondering, an execution context is simply put as the environment in which a JavaScript code is executed. An execution context consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The thread of execution and&lt;/li&gt;
&lt;li&gt;A local memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since JavaScript would be creating a whole bunch of execution contexts(or execution environments), and it has just a single thread, how does it keep track of which execution context its thread should be in and which it should return to? We simply say the &lt;code&gt;call stack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpiahcfqczl77rulnqejf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpiahcfqczl77rulnqejf.png" alt="the call stack image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens is that, when a function is executed, and JavaScript creates an execution context for that functions execution. The newly created execution context is pushed to the call stack. Now whatever is on top of the call stack is where the JavaScript thread would reside in. Initially when JavaScript runs an application and creates the &lt;code&gt;global execution context&lt;/code&gt;, it pushes this context into the call stack and since it appears to be the only entry in the call stack, the JavaScript thread lives in this context and runs every code found there. &lt;/p&gt;

&lt;p&gt;Now, the moment a function is executed, a new &lt;code&gt;execution context&lt;/code&gt; is created, this time &lt;code&gt;local&lt;/code&gt;, it is pushed into the call stack, where it assumes the top position and automatically, this is where the JavaScript thread would move to, running instructions it finds there.&lt;/p&gt;

&lt;p&gt;JavaScript knows it's time to stop executing a function once it gets to a return statement or just curly braces. If a function has no explicit return statement, it returns &lt;code&gt;undefined&lt;/code&gt;, either way, a return happens.&lt;/p&gt;

&lt;p&gt;So the moment, JavaScript encounters a return statement in the course of executing a function, it immediately knows that's the end of the function and erases the execution context that was created and at the same time, the execution context that was erased gets popped off the call stack and the JavaScript thread continues to the execution context that assumes the top position.&lt;/p&gt;

&lt;p&gt;To further illustrate how this works, let's take a look at the piece of code below, I would work us through how it is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;randomFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;generatedFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;randomFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generatedFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//4 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the little function above, I would illustrate how JavaScript runs applications and how it makes use of the call stack.&lt;/p&gt;

&lt;p&gt;The first time JavaScript runs this application if we remember the global execution context gets pushed into the call stack, for our function above the same thing happens, let's walk through it;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;global execution context&lt;/code&gt; gets created and pushed into the &lt;code&gt;call stack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;JavaScript creates a space in memory to save the function definition and assign it to a label &lt;code&gt;randomFunction&lt;/code&gt;, the function is merely defined but not executed at this time.&lt;/li&gt;
&lt;li&gt;Next JavaScript, comes to the statement &lt;code&gt;let generatedFunc = randomFunction()&lt;/code&gt; and since it hasn't executed the function &lt;code&gt;randomFunction()&lt;/code&gt; yet, &lt;code&gt;generatedFunc&lt;/code&gt; would equate to &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now, since JavaScript has encountered parenthesis, which signifies that a function is to be executed. It executes the function and from earlier we remember that when a function is executed, a new execution context is created, the same thing happens here. A new execution context we may call &lt;code&gt;randomFunc()&lt;/code&gt; is created and it gets pushed into the call stack, taking the top position and pushing the global execution context, which we would call &lt;code&gt;global()&lt;/code&gt; further down in the call stack, making the JavaScript thread to reside in the context &lt;code&gt;randomFunc()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since the JavaScript thread is inside the &lt;code&gt;randomFunc()&lt;/code&gt;, it begins to run the codes it finds within.&lt;/li&gt;
&lt;li&gt;It begins by asking JavaScript to make space in memory for a function definition which it would assign to the label &lt;code&gt;multiplyBy2&lt;/code&gt;, and since the function &lt;code&gt;multiplyBy2&lt;/code&gt; isn't executed yet, it would move to the return statement.&lt;/li&gt;
&lt;li&gt;By the time JavaScript encounters the return keyword, we already know what would happen right? JavaScript terminates the execution of that function, deletes the execution context created for the function and pops the call stack, removing the execution context of the function from the call stack. For our function when JavaScript encounters the return statement, it returns whatever value it is instructed to return to the next execution context following and in this case, it is our &lt;code&gt;global()&lt;/code&gt; execution context. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the statement,  &lt;code&gt;return multiplyBy2&lt;/code&gt;, it would be good to note that, what is returned isn't the label &lt;code&gt;multiplyBy2&lt;/code&gt; but the value of &lt;code&gt;multiplyBy2&lt;/code&gt;. Remember we had asked JavaScript to create a space in memory to store the function definition and assign it to the label &lt;code&gt;multiplyBy2&lt;/code&gt;. So when we return, what gets returned is the function definition and this gets assigned to the variable &lt;code&gt;generatedFunc&lt;/code&gt;, making &lt;code&gt;generatedFunc&lt;/code&gt; what we have below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;generatedFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are saying, JavaScript should create a space in memory for the function definition previously knowns as &lt;code&gt;multiplyBy2&lt;/code&gt; and this time assign it to the variable or label &lt;code&gt;generatedFunc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next line, &lt;code&gt;let result = generatedFunc(2)&lt;/code&gt;, we execute the function definition which &lt;code&gt;generatedFunc&lt;/code&gt; refers to (previously our &lt;code&gt;multiplyBy2&lt;/code&gt;), then this happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The variable result is equated to &lt;code&gt;undefined&lt;/code&gt; since at this time the function it references hasn't been executed.&lt;/li&gt;
&lt;li&gt;JavaScript creates another execution context we would call &lt;code&gt;generatedFunc()&lt;/code&gt;. When a local execution context is created, it consists of local memory.&lt;/li&gt;
&lt;li&gt;In the local memory, we would assign the argument &lt;code&gt;2&lt;/code&gt; to the parameter &lt;code&gt;num&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Let's not forget, the local execution context &lt;code&gt;generatedFunc()&lt;/code&gt; would get pushed into the call stack, and assuming the top position, the JavaScript thread would run every code found inside it.&lt;/li&gt;
&lt;li&gt;When JavaScript encounters the return statement, it evaluates &lt;code&gt;num * 2&lt;/code&gt;, and since &lt;code&gt;num&lt;/code&gt; refers to &lt;code&gt;2&lt;/code&gt; stored initially in local memory, it evaluates the expression &lt;code&gt;2*2&lt;/code&gt; and returns it. &lt;/li&gt;
&lt;li&gt;In returning the evaluation of the expression &lt;code&gt;2*2&lt;/code&gt;, JavaScript terminates the execution of the &lt;code&gt;generatedFunc&lt;/code&gt; function, the returned value gets stored in the variable &lt;code&gt;result&lt;/code&gt; then the call stack gets popped, removing the &lt;code&gt;generatedFunc()&lt;/code&gt; context and getting the thread back to the &lt;code&gt;global()&lt;/code&gt; context. So when we &lt;code&gt;console.log(result)&lt;/code&gt;, we get &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  In conclusion:
&lt;/h3&gt;

&lt;p&gt;The key things to take away from this article is that;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For every function execution, a new execution context is created, which gets popped into the call stack and is how the JavaScript thread learns which environment to take instruction from and execute.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Thank you for reading. If this article was helpful please give it some reactions and share, so others can find it. I will like to read your comments also.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;credits to&lt;/em&gt;&lt;/strong&gt; &lt;code&gt;FreecodeCamp&lt;/code&gt; &lt;strong&gt;&lt;em&gt;for the images used in this article&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Prototypal inheritance - A Trip back in time</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Sat, 17 Aug 2019 09:42:55 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/prototypal-inheritance-a-trip-back-in-time-2f70</link>
      <guid>https://dev.to/ogwurujohnson/prototypal-inheritance-a-trip-back-in-time-2f70</guid>
      <description>&lt;p&gt;Before classes became a thing in JavaScript, prototypes were the oop way of doing things in javascript.  This seemed to scare some folks away from javascript,  while some totally ignored it.  &lt;/p&gt;

&lt;p&gt;Currently, folks picking up javascript today avoid prototyping by all means. It would be good,  if everyone who learned Javascript during the era of es6 understood what happens behind the scene when we use classes, class Inheritance and the super() keyword in javascript.&lt;/p&gt;

&lt;p&gt;To get started with this tutorial, we would first have to revisit the basic concepts we learned starting off, one of which is &lt;code&gt;objects&lt;/code&gt;.&lt;br&gt;
Objects are key/value pairs. The most common way to create an object is with curly braces {} and you add properties and methods to an object using dot notation. In order to effectively learn about prototypes in JavaScript, we’re going to channel our minds back to &lt;code&gt;objects&lt;/code&gt; and how they are used.&lt;/p&gt;
&lt;h3&gt;
  
  
  ProtoType:
&lt;/h3&gt;

&lt;p&gt;The Prototype is the mechanism by which all JavaScript objects inherit from one another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given the constructor function person, we would want to introduce a speak property using Person.prototype&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;speak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have added the &lt;code&gt;speak&lt;/code&gt; property to the &lt;code&gt;Person&lt;/code&gt; constructor, it now completely owns it and can pass it down to each instance of &lt;code&gt;Person&lt;/code&gt;, without having to create a new property on any objects.&lt;/p&gt;

&lt;h4&gt;
  
  
  Inheritance using prototypes:
&lt;/h4&gt;

&lt;p&gt;Let's look at how inheritance works in JavaScript using prototypes. We create a &lt;code&gt;Child&lt;/code&gt; constructor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;childAttributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;childAttributes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isChild&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;childAttributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// this will be a special attribute to Child&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By writing the line of code, &lt;code&gt;Person.call(this, childAttributes)&lt;/code&gt;, we are binding &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt;, and this is what makes sure that the &lt;code&gt;Child&lt;/code&gt; constructor inherits the properties of the &lt;code&gt;Person&lt;/code&gt; constructor, this really cool process has been abstracted away by the &lt;code&gt;super&lt;/code&gt; keyword in &lt;code&gt;Es6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One problem still exists, our Inheritance isn't complete yet, because the &lt;code&gt;Child&lt;/code&gt; constructor isn't aware of the Person prototype yet. We would have to manually tell &lt;code&gt;Child&lt;/code&gt; about &lt;code&gt;Person&lt;/code&gt; using &lt;code&gt;Object.create()&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Child.prototype = Object.create(Person.prototype);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we would love to add more properties, and we want them to be particular to the &lt;code&gt;Child&lt;/code&gt; constructor, we can do so by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Baby crying...`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have linked the &lt;code&gt;Person&lt;/code&gt; prototype with the &lt;code&gt;Child&lt;/code&gt; prototype. Eventually, we’ll get this linking for free with the class keyword, but seeing &lt;code&gt;Object.create()&lt;/code&gt; is really good because it demonstrates how the &lt;code&gt;class&lt;/code&gt; keyword works under the hood.&lt;/p&gt;

&lt;p&gt;Now that we have created a Child constructor, let's introduce Ben to the family:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ben&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Ben&lt;/span&gt; &lt;span class="nx"&gt;Halpern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Comedian&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's good to note we are using the prototype’s inheritance from the &lt;code&gt;Child&lt;/code&gt; constructor to access our &lt;code&gt;Person&lt;/code&gt; properties. Now we can call ben.speak() and see what happens.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Hello, my name is Ben Halpern&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We just used inheritance from the &lt;code&gt;Person&lt;/code&gt; prototype to use ben.speak. Take a moment to reflect on that! and if you have any questions and feedback, please leave a note on the comments. &lt;/p&gt;

&lt;p&gt;Cheers 🥂🎉🎉🎉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>prototypes</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>WebHook - An Introduction</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Sat, 10 Aug 2019 07:47:49 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/webhook-an-introduction-3a7d</link>
      <guid>https://dev.to/ogwurujohnson/webhook-an-introduction-3a7d</guid>
      <description>&lt;p&gt;Most folks hate being micro-managed, we mostly want to be allowed to do our best work and not to be constantly reminded of it, or being repeatedly asked if we are done when it's obvious we aren't. This kind of behavior from direct managers and colleagues have forced some Engineers to tip their hat and resign from their positions.&lt;/p&gt;

&lt;p&gt;But how would you feel to learn that your server-side application living and chilling on AWS or Azure feels exactly the same way when you repeatedly send a request to it, asking for data, which obviously isn't ready or doesn't exist?&lt;br&gt;
I'm sure we won't want our server-side application to tip the hat and resign from its position when it can't take the repeated calls and request from us and the client-side. Little wonder, products like GitHub enforced a limit on the amount of request you could make to their server, just to be safe and sure that their server-side application stays happy and doesn't quit on them 🎩, from the possible billions of request that would come from uncontrolled access to it.&lt;/p&gt;

&lt;p&gt;Now, the question on everyone's mind would be, if we are going to do away with having to repeatedly ask the server if it has something new for us, how would we be aware of changes on our server? Or are we expecting our users to be refreshing their browsers every 10minutes to get new content? &lt;/p&gt;

&lt;p&gt;If we are running products that require reading lots of data like Twitter and GitHub, we definitely wouldn't expect our users to refresh their browsers every time they want to receive the latest information or feed in the case of Twitter, at least not in this our era of SPA's and Ajax.&lt;br&gt;
So how do we tackle this problem?&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe5q9kwz2rmi28hual47z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe5q9kwz2rmi28hual47z.gif" alt="drum roll"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Introducing the Big Man...WebHooks&lt;/strong&gt; 🥁🥁🎉🎉🎊🎊&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The introduction of WebHooks led to a new form of communication between the client-side application and the server-side application. &lt;br&gt;
Before now, the form of communication that exists between the client-side and the server-side is the one which had the client consuming the server, when making a request for a resource from the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv5o43e1eoq7fyjjl4fc4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv5o43e1eoq7fyjjl4fc4.gif" alt="consuming the backend by client"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With a WebHook, the server-side this time consumes a resource from the client-side and not the other way around(the resource being the end-point URL provided by the client-side application), which is why the WebHook is commonly referred to as the "&lt;strong&gt;Reverse API&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;The concept of a WebHook is simple. A WebHook is an &lt;code&gt;HTTP callback&lt;/code&gt; or &lt;code&gt;HTTP POST&lt;/code&gt;, that occurs when something happens; a simple event-notification via &lt;code&gt;HTTP POST&lt;/code&gt;.&lt;br&gt;
With WebHooks, we avoid the need for constant polling of the server-side application by the client-side application in search of certain resources, but rather the client-side is alerted whenever there is a change to such resources which it's interested in.&lt;/p&gt;

&lt;p&gt;Thus, rather than the client-side application constantly polling the server-side application to check for new events, the server-side application calls the client-side application (by invoking a client provided webhook URL) anytime the server-side has something new to report to the client.&lt;/p&gt;

&lt;p&gt;A WebHook has the ability of not just notifying the client-side of a change in the server-side, but can also take a payload, a payload which could be information about the new resource, but while it is possible to pass payload data within the body of the webhook, to improve scalability and reliability it is a good practice to keep the webhooks light-weight. Webhooks although technically can carry a payload, are only a callback mechanism, and primarily should serve as a signal for change in state.&lt;/p&gt;

&lt;p&gt;Once the notification in the change of state is received, a separate API call could be made to the server requesting for the new resource. This separation of concern allows for improved scalability and reliability of the API integration. And this is exactly what Twitter and LinkedIn does when they allow you to update your timeline but only show you a button to do so when there is a new resource(tweet or post)&lt;/p&gt;

&lt;p&gt;Thus, a webhook is nothing but a simple client-side provided end-point URL. This end-point URL has to be passed by the client-side application to the server-side application at some point prior to the webhook call by the server-side.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The following site allows you to test the concept of webhooks &lt;a href="https://webhook.site/" rel="noopener noreferrer"&gt;https://webhook.site/&lt;/a&gt;. It generates a unique test webhook URL that can be called by your application to test the callback (notification) feature of the webhook. You could further test it, by adding the link generated as a webHook in a GitHub repo's webhook setting and making a push to that repo and see the awesomeness of webhooks from webhook.site&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With regards to the argument about whether a webhook should have a payload, have a look at this article on &lt;a href="https://medium.com/@jsneedles/your-webhooks-endpoint-should-do-almost-nothing-d246378a85e5" rel="noopener noreferrer"&gt;medium by jsneddles&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For further read, check out &lt;a href="https://codeburst.io/whats-a-webhook-1827b07a3ffa" rel="noopener noreferrer"&gt;https://codeburst.io/whats-a-webhook-1827b07a3ffa&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;br&gt;
As this is a concept I just got to understand, it is possible, I didn't touch all aspects of it, and would love to get your kind feedbacks, questions, and contributions. Thanks.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Optimizing React Components with Error Boundaries</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Mon, 05 Aug 2019 11:52:45 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/optimizing-react-components-with-error-boundaries-2doh</link>
      <guid>https://dev.to/ogwurujohnson/optimizing-react-components-with-error-boundaries-2doh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Disclaimer: The concept discussed in this article is part of the advanced concepts of React as outlined in the official docs. But I see it as something every react developer should know and use. You would find this my view to be accurate at the end of this tutorial.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  The React we know:
&lt;/h4&gt;

&lt;p&gt;By its original design, when JavaScript errors occur in a component as little as a &lt;code&gt;Button&lt;/code&gt; component, it leads to the complete crash and failure of the react application. At the time react did not provide a way to handle this, neither was it able to recover from these errors when they occurred.&lt;/p&gt;

&lt;p&gt;But now, the React team at facebook with the advent of React 16, introduced a new concept for error handling called &lt;code&gt;error boundaries&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Introducing Error Boundaries:
&lt;/h4&gt;

&lt;p&gt;With the introduction of &lt;code&gt;error boundaries&lt;/code&gt;, components can have errors and crash in peace without having to affect the entire application state causing an application-wide crash&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://reactjs.org/docs/error-boundaries.html" rel="noopener noreferrer"&gt;React Documentation&lt;/a&gt;, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Error boundaries work like the &lt;code&gt;catch(){}&lt;/code&gt; block in JavaScript, but this time for components. Someone could say since it works like JavaScripts &lt;code&gt;catch(){}&lt;/code&gt; block, why don't we use &lt;code&gt;try/catch&lt;/code&gt; of creating error boundaries. So we could have something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      try { 
         &amp;lt;ShowButton /&amp;gt; 
      } catch(err) {
         console.log(err)
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This looks good and could solve the problem we want to solve with &lt;code&gt;Error Boundaries&lt;/code&gt;, but the thing is &lt;code&gt;try/catch&lt;/code&gt; only works for imperative code, but since we are working with react, which is declarative, react components are declarative and specify what should be rendered. But with &lt;code&gt;Error Boundaries&lt;/code&gt; the declarative nature of React is preserved.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just to add to this article, to understand the difference between imperative code and declarative code, look at these articles, &lt;a href="https://codeburst.io/declarative-vs-imperative-programming-a8a7c93d9ad2" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;br&gt;
&lt;a href="https://medium.com/front-end-weekly/imperative-versus-declarative-code-whats-the-difference-adc7dd6c8380" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Error Boundaries, can only be class components, how do I mean, you can't use functional components, to create an &lt;code&gt;error boundary&lt;/code&gt;, but a class component. &lt;/p&gt;

&lt;p&gt;For a class component to be considered an error boundary, it needs to have either (or both) of the lifecycle methods static &lt;code&gt;getDerivedStateFromError()&lt;/code&gt; or &lt;code&gt;componentDidCatch()&lt;/code&gt;. Where &lt;code&gt;getDerivedStateFromError()&lt;/code&gt; is used to render a fallback UI with the error response the developer specifies, and &lt;code&gt;componentDidCatch()&lt;/code&gt; is used to log error information, so here you could be using any log service of your choosing, or our favorite &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;To jump into code, this is what an error boundary looks like;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import React, {Component} from 'react';
 class ErrorBoundary extends Component {
    constructor(props) {
       super(props);
       this.state = { hasError: false };
    }

    static getDerivedStateFromError(error) {
       // Update state so the next render will show the fallback UI.
       return { hasError: true };
    }

    componentDidCatch(error, info) {
       // You can also log the error to an error reporting service
       logErrorToMyService(error, info);
    }

    render() {
      if (this.state.hasError) {
        // You can render any custom fallback UI
        return &amp;lt;h1&amp;gt;Something went wrong.&amp;lt;/h1&amp;gt;;
      }

      return this.props.children; 
    }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;The code example was extracted from the react documentation, and for further reads, I would suggest you likely have a look at it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With &lt;code&gt;Error boundaries&lt;/code&gt; in place in our code, we won't have a mere error in our button component render, kill the entire application, we would have complete control over such things, and the users of the application won't be left wondering what happened. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2Fkaq6GnxDlJaBq%2Fgiphy.gif%3Fcid%3D790b76115d480c6d6e704676777dfb3f%26rid%3Dgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2Fkaq6GnxDlJaBq%2Fgiphy.gif%3Fcid%3D790b76115d480c6d6e704676777dfb3f%26rid%3Dgiphy.gif" alt="confused gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the created &lt;code&gt;error boundary&lt;/code&gt;, all we have to do is, wrap any component we want to be covered by error boundary within it as so;&lt;/p&gt;



&lt;p&gt;Lastly, it's important to note that, error boundaries can't be used within event listeners. When dealing with event listeners, it's best to use the &lt;code&gt;try/catch&lt;/code&gt; block.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: How you want to use &lt;code&gt;error boundaries&lt;/code&gt;, is totally up to you, you could have your entire application wrapped in an error boundary or simply have individual components wrapped within an error boundary. But, it is still highly recommended that you just put your  in a few strategic places.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For further reads on error boundaries, I won't recommend other sources than the react documentation &lt;a href="https://reactjs.org/docs/error-boundaries.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoy using &lt;code&gt;error boundaries&lt;/code&gt; as I do. That's it from me here, if you have any questions or feedback feel free to comment or DM on &lt;a href="https://twitter.com/devopsjay" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorials</category>
    </item>
    <item>
      <title>Social Logins with Google Auth</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Tue, 09 Jul 2019 16:23:14 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/social-logins-with-google-auth-4e15</link>
      <guid>https://dev.to/ogwurujohnson/social-logins-with-google-auth-4e15</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Disclaimer: I would suggest you sit tight because I am about to show you how to easily get Google Auth integrated into your react app, seamlessly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Following the advent of a unified authentication system, such as Facebook, Google, Github Logins, people tend to prefer to use these methods to get themselves authorized on any platform having such an authentication service.&lt;/p&gt;

&lt;p&gt;In this article, we would be discussing, how we can set up one; the Google social login in a react application.&lt;/p&gt;

&lt;p&gt;Given a React Login Component with the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     import React, {Component} from 'react'

      export default class Login extends Component {
          render(){
             return(
                 &amp;lt;div&amp;gt;
                    &amp;lt;input type="text" placeholder="username"/&amp;gt;
                    &amp;lt;input type="text" placeholder="username"/&amp;gt; 
                    &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
                 &amp;lt;/div&amp;gt;
             )
          }
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We would need to install a package to assist us with setting up the Google Login feature on our application. To do this we would need to install the &lt;code&gt;react-google-login&lt;/code&gt; package.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   yarn add react-google-login || or you can use || npm i react-google-login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On successful installation of the package, we would have to import it into our project. Our project should look as so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     import React, {Component} from 'react'
     import GoogleLogin from 'react-google-login'

      export default class Login extends Component {
          render(){
             return(
                 &amp;lt;div&amp;gt;
                    &amp;lt;input type="text" placeholder="username"/&amp;gt;
                    &amp;lt;input type="text" placeholder="username"/&amp;gt; 
                    &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
                 &amp;lt;/div&amp;gt;
             )
          }
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So to use the package, all we have to do is render it in our component by adding this &lt;code&gt;&amp;lt;GoogleLogin /&amp;gt;&lt;/code&gt; below &lt;code&gt;&amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The GoogleLogin component rendered, would need some properties passed as a prop to function, these properties include: &lt;code&gt;clientId&lt;/code&gt;, &lt;code&gt;buttonText&lt;/code&gt;, &lt;code&gt;onSuccess&lt;/code&gt;, &lt;code&gt;onFailure&lt;/code&gt;. I would be discussing each of them, from the easiest to set up to the hardest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;buttonText&lt;/strong&gt;: This is simply the text that would appear on the button. And we could simply do these by assigning a text to buttonText as so: &lt;code&gt;buttonText = 'Log in With Google';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;onSuccess and onFailure&lt;/strong&gt;: These are actually methods, that serve as callbacks when a request is made to authenticate with Google, so its in these methods that you specify what happens when a response is returned by Google. For our example, an &lt;code&gt;onSuccess&lt;/code&gt; method could look like what we have below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  responseGoogle = async (response) =&amp;gt; {
    const userObject = {
      username: response.w3.ofa,
      password: 'test'
    }
    if(response.w3.ofa) {
      await localStorage.setItem("user", JSON.stringify(userObject));
      await window.location.reload();
    } else {

    }
    console.log(response);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The response sent by Google has an object called &lt;code&gt;w3&lt;/code&gt;, which houses the properties &lt;code&gt;w3&lt;/code&gt; and another property for the user email. This might be different for you, so please kindly log your response when it is returned, to pick the details you need.&lt;/p&gt;

&lt;p&gt;Because Google would not return the users password, what I normally do is assign them a default password and store this password in the database. And on another time, if the user tries to log in, all I have to do is send an object with his username and the default password back to the server, which in our case is &lt;code&gt;test&lt;/code&gt;.  In our example, we would be storing the &lt;code&gt;userObject&lt;/code&gt; in &lt;code&gt;local storage&lt;/code&gt;, but if you are persisting your data in a database, you would have to send this object to the method handling the authentication of the user on the server. To complete the process, we would now assign the method created to the &lt;code&gt;onSuccess&lt;/code&gt; property as so; &lt;code&gt;onSuccess = responseGoogle;&lt;/code&gt;. For the onFailure property, we can have an alert or log inside a method that outputs an error message and we would assign this method to the &lt;code&gt;onFailure&lt;/code&gt; property as we did to the &lt;code&gt;onSuccess&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;clientId&lt;/strong&gt;: &lt;br&gt;
To get this information, you need to visit the Google API console &lt;a href="https://console.developers.google.com" rel="noopener noreferrer"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on the credential section, and click on the create credential button
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9p0bmwz0bmd61dahrtgq.png" alt="credentials" width="800" height="107"&gt;
&lt;/li&gt;
&lt;li&gt;Select the Oauth option
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwkn88vdlehc5j09jnjt.png" alt="select 0auth" width="800" height="497"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Note, if you don't a project created before now, it would prompt you to, follow the directions and create the project, then try to generate the credentials again. This time follow the prompt till you get to the part where you are shown your &lt;code&gt;clientId&lt;/code&gt;, copy the clientId and assign our own &lt;code&gt;clientId&lt;/code&gt; to it. At the end of the day, our &lt;code&gt;Login&lt;/code&gt; Component would look as so:&lt;/p&gt;

&lt;p&gt;import React, {Component} from 'react'&lt;br&gt;
     import GoogleLogin from 'react-google-login'&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  export default class Login extends Component {

      responseGoogle = async (response) =&amp;gt; {
         const userObject = {
            username: response.w3.ofa,
            password: 'test'
         }
         if(response.w3.ofa) {
            await localStorage.setItem("user", JSON.stringify(userObject));
            await window.location.reload();
         } else {

      }
      console.log(response);
      }
      render(){
         return(
             &amp;lt;div&amp;gt;
                &amp;lt;input type="text" placeholder="username"/&amp;gt;
                &amp;lt;input type="text" placeholder="username"/&amp;gt; 
                &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
                &amp;lt;GoogleLogin
                  clientId="##########" //id gotten from Google
                  buttonText="Log in with Google"
                  onSuccess={this.props.responseGoogle}
                  onFailure={this.props.responseGoogleError}
                /&amp;gt;
             &amp;lt;/div&amp;gt;
         )
      }
  }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that we have our React application authenticated with Google 0auth. This is as simple as it comes, if further clarification is needed, please feel free to reach out to me, here or via Google Mail 😂&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Introduction to Styled Components</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Sun, 16 Jun 2019 20:07:09 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/introduction-to-styled-components-3i7e</link>
      <guid>https://dev.to/ogwurujohnson/introduction-to-styled-components-3i7e</guid>
      <description>&lt;p&gt;This article would have us jumping straight to introducing the concepts and approaches of using styled components in styling our React components.&lt;/p&gt;

&lt;p&gt;Styled components follow the paradigm of CSS-in-JS. To use styled components, first, we would have to install and import it into our react project since it doesn’t come with react pre-built.  &lt;/p&gt;

&lt;p&gt;Our normal react application looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     export default function Component() {
        return (
         &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt;Hello World &amp;lt;/p&amp;gt;
         &amp;lt;/div&amp;gt;
        )
     } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now instead of styling, this React component by littering everywhere with classNames, we could use styled components to style the components. Styled components make sure that styles don’t leak from one component to another. &lt;br&gt;
To get started using styled-components,  we would have to import it first into our project as so&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import styled from 'styled-components';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;To style the div  in our component above, we would do the following&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const DivWrapper = styled.div`
      width: 50%;
      border: 2px solid black;
    `;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The variable is named as so, starting with a capital letter, because it’s actually a component. Now we would have to replace our &lt;code&gt;div&lt;/code&gt; with the &lt;code&gt;DivWrapper&lt;/code&gt; we just created. Below is the code demonstrating how:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     export default function Component() {
     return (
      &amp;lt;DivWrapper&amp;gt;
        &amp;lt;p&amp;gt;Hello Styled component&amp;lt;/p&amp;gt;
      &amp;lt;/DivWrapper&amp;gt;
     )
     }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;By doing this, the styled associated with &lt;code&gt;DivWrapper&lt;/code&gt; is applied to our component. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s wise to note that, styled components should never be declared within a class or function component but outside the scope of both. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We could go further to style the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag by doing  these:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     const Paragraph = styled.p`
       font-size: 32px;
     `;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;With these we could replace the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag with the Paragraph styled component and the styles are applied. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Styled components support some syntaxes that come baked into preprocessors like Less and Sass, such as nesting and creation of mixins.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Just like any other react component, the styled-component receives props and gives the developer the ability to make certain style decisions with regards to what is being passed as a prop to the styled-component. To illustrate this, say our &lt;code&gt;DivWrapper&lt;/code&gt;  styled wrapper, was going to be re-usable by other components, then the need would arise for us to specify certain style guidelines that would differ based on the choice of the developer, one of such could be the background color of the divs. How can we do this with styled-components?&lt;/p&gt;

&lt;p&gt;First, we would need to pass to our styled-component a prop of color as so:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;DivWrapper
    color= 'blue'
  &amp;gt;

  &amp;lt;/DivWrapper&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now in our styled-component declaration, we would do the following:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const DivWrapper = styled.div`
    width: 50%;
    border: 2px solid black;
    ${props =&amp;gt; (props.color === 'blue') ? `background-color: blue`: null}
    ${props =&amp;gt; (props.color === 'red' ? `background-color: red`: null)}
  `;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Styled components can inherit from each other, how do we mean. A styled component can re-use the styles present in another styled component.  To illustrate, say we have another div but this time it has a background color of red, instead of creating a totally different styled component, we can create one that inherits all the properties of the former div styled component &lt;code&gt;DivWrapper&lt;/code&gt; and then add the additional styles it relies on. Here is how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const DivWrapper2 = styled(DivWrapper)`
        background-color: 'blue';
    `;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we use the styled component &lt;code&gt;DivWrapper2&lt;/code&gt;, it inherits the styles,  width, and border from the &lt;code&gt;DivWrapper&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;Styled components are great for styling React components.  If you are looking at digging deep into styled components, visit the &lt;a href="https://www.styled-components.com/"&gt;official site&lt;/a&gt; here.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Introduction to Higher Order Components(HOCs)In React</title>
      <dc:creator>Johnson Ogwuru</dc:creator>
      <pubDate>Sun, 09 Jun 2019 18:17:51 +0000</pubDate>
      <link>https://dev.to/ogwurujohnson/introduction-to-higher-order-components-hocs-in-react-273f</link>
      <guid>https://dev.to/ogwurujohnson/introduction-to-higher-order-components-hocs-in-react-273f</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The past week got me learning a lot of things, a lot of concepts I would really love to share. I would begin by discussing Higher-order functions(HOCs) In react. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To be able to understand Higher order components in react, we would first need to understand what Higher Order functions are in JavaScript. Higher-order functions in Javascript are functions that take other functions as arguments and return another function. Examples of the higher-order functions are the; &lt;code&gt;.map&lt;/code&gt;, &lt;code&gt;.filter&lt;/code&gt;, etc. Some of them I wrote about in this article:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/ogwurujohnson" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UqO2JzFU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--I8bvH3GE--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/45918/1caee5a7-9391-4900-ae00-31e6dd6dfb13.jpg" alt="ogwurujohnson"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/ogwurujohnson/foreach-map-filter--whats-the-difference-304l" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;.forEach(), .map(), .filter() .... What's the difference?&lt;/h2&gt;
      &lt;h3&gt;Johnson Ogwuru ・ May 11 '18 ・ 1 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;It’s important to note that  I’m assuming you have prior knowledge of React if you don’t below is an awesome tutorial by traverse media.&lt;br&gt;
That aside let’s jump right in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/sBws8MSXN7A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Higher Order Components are components that take as argument another component or components and returns a new component. Could be likened to a Higher Order function, well that’s where the name came from. It is an advanced technique in react for re-using the logic of a component. HOC’s could be used when we want to abstract a login component from the other components in a site, and I would demonstrate how. HOCs are commonly used as advanced react patterns for designing some components that share certain behaviors in a way that makes them connected differently than just the normal state-&amp;gt; props pattern. HOCs as an advanced react pattern allows us the capability of reusing component logic. Think about the functionality of the login component I mentioned. The login could be just a form that takes as value a username, and password and performs a check against a database or any storage means if the user exists on the system. if the user does,  and the password entered is correct,  then the App component would render the content component, but if the user doesn’t exist, the App renders the login component, the same could be applied to when checking if a user on the site still has a valid authentication. This example could be performed using a Higher-order component. But how, the code below demonstrates how.&lt;/p&gt;

&lt;p&gt;Say we have an App component as such&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   import React, { Component } from 'react'

   //below is the app component that would render to the DOM
   export default class App extends Component {
     render() {
       return (
          &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt; Hello world &amp;lt;/h1&amp;gt;
          &amp;lt;/div&amp;gt;
       )
     }
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To create a higher order component that would perform the function we want, which is determining which component to render depending on the status of the user's login session, we would first have to create the two components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note, to make this explanation as simple as it could possibly get, I would be creating all of the components inside the app.js file. And also we would be making use of create-react-app, please get it set up&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below, we would be creating both the login and content components, both are actually going to be dummy components simply calling out their names in an &lt;code&gt;h1&lt;/code&gt; tag.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   import React, { Component } from 'react'

   // Login Component
   export default function Login {
      return (
         &amp;lt;div&amp;gt;
           &amp;lt;h1&amp;gt;Hi i'm login&amp;lt;/h1&amp;gt;
         &amp;lt;/div&amp;gt;
      )
   }

   //Content component
   export default function Content {
       return (
          &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Hi i'm Content&amp;lt;/h1&amp;gt;
          &amp;lt;/div&amp;gt;
       )
   }

   //App component
   export default class App extends Component {
     render() {
       return (
          &amp;lt;div&amp;gt;
            //Add component to render here
          &amp;lt;/div&amp;gt;
       )
     }
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Having built out the individual components, it's time for us to create our HOC.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that you can choose to create this in a different file, but to make this article simple and straight to the point, I would be doing everything from a single file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Copy and paste the following code immediately under the &lt;code&gt;Content&lt;/code&gt; component and before the &lt;code&gt;App&lt;/code&gt; component.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const withAuthentication = Component1 =&amp;gt; Component2 =&amp;gt;
     class extends Component {
       constructor(props) {
         super(props);
         this.state = {
          isLoggedin: false,
         };
       }

       render(){
          if(this.state.isLoggedin) {
            return &amp;lt;Component1 /&amp;gt;
          } else {
            return &amp;lt;Component2 /&amp;gt;
          }

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

&lt;/div&gt;

&lt;p&gt;The above is as simple as a Higher-order Component can be.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's of vital importance that we note how &lt;code&gt;HOCs&lt;/code&gt; are named, it's generally accepted that they are named starting with &lt;code&gt;with&lt;/code&gt; and then whatever their specific job is, In our case authentication. That way anyone looking at your code knows it's a Higher-order component without looking at the code, this is especially very important where the &lt;code&gt;HOC&lt;/code&gt; leaves in a separate file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To explain the code. We use what is being referred to as &lt;code&gt;curring&lt;/code&gt; in JavaScript to receive the arguments supplied to a &lt;code&gt;HOC&lt;/code&gt;. Currying was what we did when we passed the two arguments to the &lt;code&gt;HOC&lt;/code&gt; as so; &lt;code&gt;Component1 =&amp;gt; Component2 =&amp;gt;&lt;/code&gt;, which is actually equivalent to us declaring a method and passing an argument to it, the normal Javascript way.&lt;/p&gt;

&lt;p&gt;Further down, the HOC returns an anonymous class which has a state object, that houses the &lt;code&gt;isLoggedin&lt;/code&gt; property, which you would manipulate however you want, during the course of developing your application, to determine when a user is logged in and when they are not.&lt;/p&gt;

&lt;p&gt;Further down to the &lt;code&gt;render()&lt;/code&gt; method, we add a condition to check the state for the value of &lt;code&gt;isLoggedin&lt;/code&gt;, so whatever the value is a Component is being returned respectively.&lt;/p&gt;

&lt;p&gt;Now, the question in your mind could obviously be how you can get to use this. Pay close attention. Copy and paste the following code immediately under the &lt;code&gt;HOC withAuthentication()&lt;/code&gt; we just wrote.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const DoAuthenticate = withAuthentication(Content, Login);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now by assigning our &lt;code&gt;HOC withAuthenticate&lt;/code&gt; to a function &lt;code&gt;DoAuthenticate&lt;/code&gt;, we are setting whatever the returned function inside of HOC to &lt;code&gt;DoAuthenticate&lt;/code&gt; who from how it's named, shows it's a component. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because of how our HOC was organized, we need to make sure the &lt;code&gt;Content&lt;/code&gt; component stays at the position of &lt;code&gt;Component1&lt;/code&gt; in the &lt;code&gt;HOC&lt;/code&gt; because we expect it to be returned when the user is logged and the &lt;code&gt;Login&lt;/code&gt; component taking the position of &lt;code&gt;Component2&lt;/code&gt; as defined in the &lt;code&gt;HOC&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now instead of having to wonder which Component to render to the &lt;code&gt;DOM&lt;/code&gt; through the App Component, we just render the &lt;code&gt;DoAuthenticate&lt;/code&gt; and allow the &lt;code&gt;HOC&lt;/code&gt; do the pondering over for us. In the end, our code should look like so.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   import React, { Component } from 'react'

   // Login Component
   export default function Login {
      return (
         &amp;lt;div&amp;gt;
           &amp;lt;h1&amp;gt;Hi i'm login&amp;lt;/h1&amp;gt;
         &amp;lt;/div&amp;gt;
      )
   }

   //Content component
   export default function Content {
       return (
          &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Hi i'm Content&amp;lt;/h1&amp;gt;
          &amp;lt;/div&amp;gt;
       )
   }


   // Higher order Component
   const withAuthentication = Component1 =&amp;gt; Component2 =&amp;gt;
     class extends Component {
       constructor(props) {
         super(props);
         this.state = {
          isLoggedin: false,
         };
       }

       render(){
          if(this.state.isLoggedin) {
            return &amp;lt;Component1 /&amp;gt;
          } else {
            return &amp;lt;Component2 /&amp;gt;
          }

       }


   const DoAuthenticate = withAuthentication(Content, Login);

   //App component
   export default class App extends Component {
     render() {
       return (
          &amp;lt;div&amp;gt;
            &amp;lt;DoAuthenticate /&amp;gt;
          &amp;lt;/div&amp;gt;
       )
     }
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Learning about HOCs was really fun for me and I made two applications during the week, to deepen my understanding of it. Here are links to the applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://instaclonex.netlify.com/"&gt;Instaclone&lt;/a&gt; a clone of the Instagram application(not a complete clone though...lol) and &lt;a href="https://lambdatimex.netlify.com/"&gt;LambdaTimeX&lt;/a&gt;. Would be dropping links to they Repos on the comment section.&lt;/p&gt;

&lt;p&gt;To further your knowledge, I would recommend you give this article on HOCs by &lt;a href="https://www.sitepen.com/blog/higher-order-components-in-react/"&gt;Sitepen&lt;/a&gt; a shot. 🥂🥂🥂🥂&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
