<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ewere Ebie on Medium]]></title>
        <description><![CDATA[Stories by Ewere Ebie on Medium]]></description>
        <link>https://medium.com/@caleb-codes?source=rss-8bf526ce4814------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*r0U9--8iUmAUiXG4lZUQKA.jpeg</url>
            <title>Stories by Ewere Ebie on Medium</title>
            <link>https://medium.com/@caleb-codes?source=rss-8bf526ce4814------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:47:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@caleb-codes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker-compose to the rescue]]></title>
            <link>https://medium.com/geekculture/docker-compose-to-the-rescue-820d9c7b364c?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/820d9c7b364c</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Thu, 11 May 2023 20:09:02 GMT</pubDate>
            <atom:updated>2023-05-22T04:38:56.808Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*LcPDVOJfEynOSxyX.jpeg" /></figure><p>Creating containers using native Docker commands, as we demonstrated in our <a href="https://medium.com/@caleb-codes/connecting-multiple-containers-using-docker-network-74b5ebb49075">previous article</a>, gives a solid understanding of how Docker works. However, as service requirements increase, it can become cumbersome to manage them in this manner.</p><p><strong>Docker Compose</strong> simplifies the description of services, volumes, and networks in a YAML file, allowing us to manage application lifecycles, including image building and container execution.</p><p>Our goal today is to create the docker resources needed for our Bambi event manager app using docker compose. Let’s go! 😤</p><h3>Prerequisites</h3><p>To complete this lesson you should have</p><ul><li><a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">Git</a></li><li><a href="https://docs.docker.com/desktop">Docker Desktop</a> — Has Docker CLI and Docker compose.</li><li>Functioning code editor (e.g <a href="https://code.visualstudio.com/download">Vscode</a>)</li></ul><h3>Step 1 — Pull down application</h3><p>Open your terminal on the folder you wish to have this repository and pull down the repo with these commands</p><pre>git clone https://github.com/caleb-42/event-manager-node.git<br>cd event-manager-node<br>git checkout use-docker-compose</pre><p>once it’s complete open the folder with your code editor.</p><h3>Step 2 — Create .env file using env-sample file</h3><p>There is a file named env-sample in the project directory. Use its content to create a <strong>.env</strong> file for the event application.</p><pre>PORT=3210<br>NODE_ENV=development<br>API_KEY=myapikey<br>DATABASE_URL=postgres://postgres:password@postgres-db/event_manager<br>JWT_SECRET=secret</pre><h3>Step 3 — Create Docker compose file</h3><p>Create a file called <strong>docker-compose.yml</strong> at the root of your application and paste in the yaml below .</p><pre>version: &quot;3.4&quot;<br>services:<br>  event-manager:<br>    container_name: event-manager-app<br>    env_file: ./.env<br>    build:<br>      context: .<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3210:3210&quot;<br>    networks:<br>      - event-manager-network<br>networks:<br>  event-manager-network:<br>    driver: bridge</pre><p><strong>version</strong> defines the docker-compose release that supports the schema in the file, while <strong>services</strong> is a top level property that has listing of containers you plan to run. networks property defines a bridge network called event-manager-network which every container will get added to to enable communication.</p><blockquote>Docker compose creates a bridge network by default that enable communication between containers. The networks property allows us create our own custom networks.</blockquote><p>One of the ways to pass environment variables to a container is to use the env_file property. we assign .env file we created previously to it.</p><p>The build property specifies the context(folder location) to find the Docker build file and the <strong>dockerfile</strong> property states the file name. The port key maps a host port(3210) to our container port(3210).</p><h3>Step 4 — Add postgres-db to docker-compose file</h3><p>Before running our application we need to include our Postgres database so the event manager can persist data. Now your <strong>docker-compose.yml </strong>file should look like this 👇.</p><pre>version: &quot;3.4&quot;<br>services:<br>  event-manager:<br>    container_name: event-manager-app<br>    env_file: ./.env<br>    build:<br>      context: .<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3210:3210&quot;<br>    networks:<br>      - event-manager-network<br>  postgres-db:<br>    container_name: event-manager-db<br>    image: postgres:14.1-alpine<br>    restart: always<br>    environment:<br>      POSTGRES_USER: &quot;postgres&quot;<br>      POSTGRES_PASSWORD: &quot;password&quot;<br>      POSTGRES_DB: &quot;event_manager&quot;<br>    ports:<br>      - &quot;5434:5432&quot;<br>    volumes:<br>      - db:/var/lib/postgresql/data<br>      - ./db.sql:/docker-entrypoint-initdb.d/db.sql<br>    networks:<br>      - event-manager-network<br>volumes:<br>  db:<br>    driver: local<br>networks:<br>  event-manager-network:<br>    driver: bridge</pre><p>Docker compose will pull an image from Dockerhub using the value given to the <strong>image</strong> key. The <strong>restart</strong> key with a value of “always” means that if the container stops arbitrarily docker compose will spin up a new one.</p><p>Using <strong>environment</strong> key allows us pass environment variables to a container using key-value pairs. The variables of importance to the postgres-db container are the <strong>POSTGRES_USER</strong>, <strong>POSTGRES_PASSWORD</strong>, <strong>POSTGRES_DB</strong>.</p><blockquote>While sufficient for local development, it is not recommended to pass sensitive information plainly to your Postgres container in <strong>production</strong>.</blockquote><p>the <strong>port</strong> key maps a host port(5434) to our container port(5432) granting us the flexibility of escaping port conflict errors.</p><p>We use the <strong>volumes</strong> key to mount a <strong><em>db</em></strong> volume to the <strong><em>/var/lib/postgresql/data</em></strong> location in the container so we can persist the data. <br>The <strong>db.sql</strong> initialisation script is mounted to the <strong>docker-entrypoint-initdb.d</strong> location in our container so it runs the moment the container starts.</p><p>Lastly we add the postgres-db to the event-manager-network.</p><h3>Step 5 — Build Docker Images with compose</h3><p>With the docker-compose file ready we can now build our image with the command below</p><pre>docker-compose build</pre><p>The outcome of a successful build should be the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*jPt3mFPhy0Slcene.png" /></figure><h3>Step 5 — Run Docker container with compose</h3><p>The last step is to create a container using our image. Use the command below to do so.</p><pre>docker-compose up</pre><p>The outcome of a successful build should be the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*uSbrejz9L-_Ipg8s.png" /></figure><p>Now if you navigate to <a href="http://localhost:3210y">http://localhost:3210</a> you will see the app running and data displayed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*vqodUznbxx9ZNRM6.png" /></figure><h3>Conclusion</h3><p>By utilising Docker Compose, we effectively managed our services without the need for repetitive Docker commands on the terminal. In my upcoming article, I’ll delve into shelling into containers and handling logs for debugging purposes. Stay tuned and see you next time!</p><blockquote><em>I’m glad you took the time to read my blog. If you liked it leave a </em><strong><em>clap</em></strong><em> and follow my medium account for more content like this. Thanks 👋.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=820d9c7b364c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/docker-compose-to-the-rescue-820d9c7b364c">Docker-compose to the rescue</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting multiple containers using Docker network]]></title>
            <link>https://medium.com/geekculture/connecting-multiple-containers-using-docker-network-74b5ebb49075?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/74b5ebb49075</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-networking]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Thu, 11 May 2023 20:08:05 GMT</pubDate>
            <atom:updated>2023-05-14T10:00:54.458Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/430/0*C3_cKxvgdm4ZrgkI.jpeg" /></figure><p>In addition to isolating applications, Docker also offers a means to streamline container communication through the use of <strong>Bridge networks</strong>. When you create a user-defined bridge network, containers within the network can resolve each other by aliases, similar to DNS names. This functionality enhances the ease of communication between containers within the Docker environment.</p><p>In this article, we will demonstrate how to set up two containers: one for our full-stack application, <strong>Bambi Event Manager</strong>, and another for a Postgres database. Let’s get started! 😤.</p><h3>Prerequisites</h3><p>To complete this lesson you should have</p><ul><li><a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">Git</a></li><li><a href="https://docs.docker.com/get-docker">Docker</a></li><li>functioning code editor (e.g <a href="https://code.visualstudio.com/download">Vscode</a>)</li></ul><h3>Step 1 — Pull down application</h3><p>Open your terminal on the folder you wish to have this repository and pull down the repo with the command</p><pre>git clone https://github.com/caleb-42/event-manager-node.git</pre><p>once it’s complete open the folder with your code editor.</p><h3>Step 2 — Create Docker bridge network</h3><pre>docker network create event-manager-network</pre><p>When you run this command it will create a bridge network that will enable us link our database container to the event-manager-app container when they are created.</p><h3>Step 3 — Setup Postgres docker image &amp; volume</h3><p>To create our database we will use a Postgres image from Dockerhub.</p><pre>docker pull postgres:14.1-alpine</pre><p>Before creating a Postgres container with this image we need to create a file at the root of our project called <strong><em>db_env.</em></strong> This file will hold our environment variables.</p><pre>POSTGRES_USER=postgres<br>POSTGRES_PASSWORD=password<br>POSTGRES_DB=event_manager</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*W2sLoqC3pO2X_3bk.png" /></figure><p>Now your folder structure should look this way. Pay close attention to the <strong>db_env</strong> and <strong>db.sql</strong> files as they are key to setting up our Postgres container.</p><p>To make our container data persistent we need to create a volume using the command below.</p><pre>docker volume create db</pre><h3>Step 4 — Create Postgres database container</h3><pre>docker run -d -p 5434:5432 --name postgres-db --network event-manager-network --env-file ./db_env -v db:/var/lib/postgresql/data -v $PWD/db.sql:/docker-entrypoint-initdb.d/db.sql postgres</pre><p>The command above will not only create a Postgres container but will map the database instance to port 5434 on our host so we can view it with a postgres client. We gave our container a name “postgres-db” and added it to the event-manager-network.</p><p>We passed environment variables from our <strong>env file</strong> to the container in order to define the Database name, user and password. The postgres data of the container was mounted to the db volume we created in order to persist data. And Lastly we mounted our <strong>db.sql</strong> initialisation script to the docker-entrypoint of our Postgres container so it runs the moment <strong>postgres-db </strong>starts. This is how our database should look on a client</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*zrz5EB5B9gojv_7C.png" /></figure><h3>Step 5 — Create docker file for event manager</h3><p>Create a file titled “Dockerfile<strong>”</strong> at the root folder of your project and populate it with the following content.</p><pre>FROM node:16.0.0-alpine<br>WORKDIR /app<br>COPY ./package.json ./<br>RUN yarn install --pure-lockfile<br>COPY ./ ./<br><br>EXPOSE 3210<br><br>CMD [&quot;yarn&quot;, &quot;start&quot;]</pre><p>This file will enable us generate an image that will be used to create a container for our application. For more details check out this <a href="https://caleb-codes.medium.com/containerising-a-react-application-a118d5c01c62"><strong>article</strong></a>.</p><h3>Step 6 — Create .env file using env-sample file</h3><p>There is a file named env-sample in the project directory. Use its content to create a <strong>.env</strong> file for the event application.</p><pre>PORT=3210<br>NODE_ENV=development<br>DATABASE_URL=postgres://postgres:password@postgres-db/event_manager<br>JWT_SECRET=secret</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*Iro2dbZmAIB0me-y.png" /></figure><p>Get <strong>API_KEY</strong> value by signing up to <a href="https://generated.photos/">https://generated.photos</a>. You can also leave it empty because it’s not mandatory. The <strong>DATABASE_URL</strong> is the most important variable and its a connection string to our database. We used the host name <strong>postgres-db</strong> because when we add containers to the same network their given names serve as domain names for communication.</p><h3>Step 7 — Build docker image for event manager</h3><pre>docker build . -t event-manager-image</pre><p>Run the above command to create an image and if yours completes successfully like mine you should see an output like the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*1aSDLjE4OUx70R1A.png" /></figure><h3>Step 8 — Run event manager docker container</h3><pre>docker run -p 3000:3210 --name event-manager-app --network event-manager-network --env-file ./.env event-manager-image</pre><p>We named our app event-manager-app and added it to the event manager network which also contains the <strong>postgres-db</strong> database app. We also passed the env file we created to set the environment variables of our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*6uELEusCbCcZtAqH.png" /></figure><p>Once the container is created you should see the output above stating our server is listening on port 3210 but if you try to get to localhost port 3210, The app will not display. Why? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*rV9JSBfw6Fkkoz5s.png" /></figure><p>Thats because we mapped our container port 3210 to display on port 3000 on the host machine while running our container start command. You could as well have used the same port on the host, but I did this to make clear the difference between them. So the right url to visit would be <a href="http://localhost:3000./">http://localhost:3000</a> and Viola!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-_HfgUSATxGvlJan.png" /></figure><h3>Conclusion</h3><p>After some effort, we successfully created bridge networks — hurray! But lets face it, relying solely on plain Docker commands for running multiple connected containers can be quite cumbersome. In my next article we’ll simplify container management using <strong>Docker compose</strong>. Caio 👋</p><blockquote><em>I’m glad you took the time to read my blog. If you liked it leave a </em><strong><em>clap</em></strong><em> and follow my medium account for more content like this. Thanks 👋.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74b5ebb49075" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/connecting-multiple-containers-using-docker-network-74b5ebb49075">Connecting multiple containers using Docker network</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Containerising a React application]]></title>
            <link>https://medium.com/geekculture/containerising-a-react-application-a118d5c01c62?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/a118d5c01c62</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Fri, 05 May 2023 22:10:36 GMT</pubDate>
            <atom:updated>2023-05-05T22:11:07.647Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*83j0KZKzLumdUlyB" /></figure><p>We will be working with a Gallery application made with create-react-app called <a href="https://github.com/caleb-42/SnapShot">snapshot</a>. In the end, we should have our app running within a container and accessible on our host machine from a designated port.</p><h3>Prerequisites</h3><p>To complete this lesson you should have</p><ul><li><a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">Git</a></li><li><a href="https://www.docker.com/products/docker-desktop/">Docker Desktop</a></li><li>functioning code editor (e.g <a href="https://code.visualstudio.com/download">Vscode</a>)</li></ul><h3>Step 1 —Pull down the repository</h3><p>Open your terminal on the folder you wish to have this repository and pull down the repo with the command</p><pre>git clone <a href="https://github.com/Yog9/SnapShot.git">https://github.com/Yog9/SnapShot.git</a></pre><p>once it&#39;s complete open the folder with your code editor.</p><h3>Step 2— Designing an Image</h3><p>The first step to creating a container is to first design an Image. What is an image? It is the step-by-step guide that docker follows to initialize a container. For better understanding think of it as the <strong><em>blueprint </em></strong>for your container just like a <strong><em>class</em></strong> is for an o<strong><em>bject</em></strong> in Object oriented programming.</p><p>Create a <strong>dockerfile</strong> with your editor at the root folder of your project and populate it with the following content.</p><pre>FROM node:16.0.0-alpine<br><br>WORKDIR /app<br>COPY ./package.json ./yarn.lock ./<br>RUN yarn install --pure-lockfile<br>COPY ./ ./<br><br>EXPOSE 3000<br><br>CMD [&quot;yarn&quot;, &quot;start&quot;]</pre><p>In the <strong>first line</strong>, we are pulling the image <strong>node:16.0.0-alpine</strong> from Docker hub so we can base our image on it. This will save us time as the image is prepackaged with a Linux distribution as well as Node.js and some other utilities like yarn. I used version 16.0.0 just out of preference and because it’s stable.</p><p>In the <strong>second line</strong>, we create and <em>enter</em> a working directory called <strong>app</strong> within our image that will serve as the root folder for our project when the container is created.</p><p>In the <strong>third line</strong>, we copy the dependency files(<em>package.json</em> and <em>yarn.lock</em>) into the app folder(./).</p><p>Installation of the necessary dependencies happens in the <strong>fourth line</strong>, before copying the rest of the project into the image in the <strong>fifth</strong> <strong>line</strong>.</p><p>We request port 3000 to be exposed for access during container creation by the host machine at the <strong>sixth line </strong>because when the application starts it will run on port 3000. This will enable us to reach the app within the container from a browser.</p><p>And lastly, on the <strong>seventh line</strong>, we specify the start script located in package.json as the instruction to be executed when the docker container starts.</p><h3>Step 3 — Building an Image</h3><p>Now we have our docker file ready to go it&#39;s time to create our image. Run the command below from a terminal pointed at the root of your project where your Dockerfile is located.</p><pre>docker build . -t snapshot-image</pre><p><strong><em>docker build</em></strong> is the actual command while the <strong>period sign</strong>(.) after it specifies the folder context it should expect to find a Dockerfile. The tag flag(<strong>-t</strong>) is used to pass in the name(<strong>snapshot-image</strong>) docker will associate with your image to make it easily identifiable. Once this is done you can confirm your image has been created by running the command below.</p><pre>docker image ls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*3oCBAsv0Ys4sDCnx-qiRiQ.png" /></figure><h3>Step 4— Creating a container</h3><p>Finally, it&#39;s time to start up our application by instantiating our image to create a container. Paste the command below to create a new container.</p><pre>docker run -it -p 4300:3000 --name snapshot-app snapshot-image</pre><p><strong><em>docker run</em></strong> is the actual command, the flag <strong>-it</strong> tells docker to remain interactive allowing us to see the output of our app’s <em>start command</em> from within the container in our terminal.</p><p><strong><em>-p 4300:3000</em></strong> is used to map the container port to a port on our host machine. 3000 is the port we exposed in our docker file and is the port our react app is running within our container, while 4300 is the host port we want to use in reaching our application from a browser.</p><p><strong><em>-name snapshot-app</em></strong> is used to give the container the name “snapshot-app” and we append the image name “snapshot-image” from which to create our container.</p><p>To confirm your container is running open another terminal and run the command below, you should see the container name listed in the output</p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mgGBiXYk-9t9vgL9S7YgGw.png" /></figure><p>Now the container is up and running you should see an output stating the code was compiled successfully along with links to the running application like so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*E7UxRpdaNlG1Fk8rdovuNw.png" /></figure><p>If you try to visit these links you would notice the site can’t be reached. This is because the app was started on port 3000 within our container but is exposed on port 4300 on our host machine. I did this port mapping purposely to prove to you that the application is not running on our host environment but is working from an isolated space (container) running a Linux distribution.</p><p>To reach our app we must use the right link <a href="http://localhost:4300/SnapShot"><strong>http://localhost:4300/SnapShot</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M_ZZcLjrE0dOKOb8V4z3sQ.png" /></figure><p>Viola!!!</p><p>We have successfully containerised a react application. 😁</p><p>Once you commit your docker file and push it to a repo anyone can start up the application from whatever Operating system running docker and expect it to behave the same every time.</p><h3>Wait… There’s more</h3><p>If we make a change to a file in our project the development server running in the container will not refresh because the app we started is isolated from the host. We need a way to maintain a connection between our project directory and that of the container. This is where <strong>bind mounts and volumes</strong> come in.</p><p><strong>Bind mounts</strong> link a directory on the host machine to one within the container allowing host file modification automatically reflect so we don&#39;t need to rebuild an image and restart a container again.</p><p>Kill the running container with these commands</p><pre>docker stop snapshot-app &amp;&amp; docker rm snapshot-app</pre><p>To bind our project directory to one within a new container we need to get the directory path of our project on the host machine. For windows use the <strong>cd</strong> command and for UNIX systems <strong>pwd</strong> will work as well as the env variable <strong>$PWD.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*XhlJMinEZkatCYZZLda6-g.png" /></figure><pre>docker run -it -p 4300:3000 --name snapshot-app --rm -v /app/node_modules -v /Users/mac/Documents/tuts/SnapShot:/app snapshot-image</pre><p>the <strong><em>-v</em></strong> flag is used to create an <strong>anonymous volume</strong> for the <strong>node_modules</strong> folder within the container and bind our host to the container. We need an anonymous volume because when we create a host volume the contents of the container will be replaced by the contents of the host project folder which has no node_modules in it.</p><p>The <strong><em>— rm</em></strong> flag is used so when the container is removed it will also delete the anonymous volume.</p><p>When you make changes to your project, it should refresh the container build and reflect on the browser. Hurray!!! 🎉</p><h3>Conclusion</h3><p>We’ve seen the process of creating containers with images and associating them with volumes. This setup will enable your projects to become more stable and predictable in the long run.</p><blockquote>I’m glad you took the time to read my blog. If you liked it give me a <strong>clap</strong> and follow my medium account for more content like this. Thanks <em>👋</em>.</blockquote><h3>References</h3><ul><li><a href="https://docs.docker.com/">Docker Docs</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a118d5c01c62" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/containerising-a-react-application-a118d5c01c62">Containerising a React application</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Frontend Mastery — How the internet works]]></title>
            <link>https://medium.com/geekculture/frontend-mastery-how-the-internet-works-56b184316d8d?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/56b184316d8d</guid>
            <category><![CDATA[tcp-ip]]></category>
            <category><![CDATA[client-server]]></category>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[dns]]></category>
            <category><![CDATA[frontend-mastery]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Sat, 18 Dec 2021 14:11:03 GMT</pubDate>
            <atom:updated>2023-05-06T22:04:57.026Z</atom:updated>
            <content:encoded><![CDATA[<h3>Frontend Mastery — How the internet works</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*vRrwGKYj_R6uYi6C.jpg" /></figure><p>For a tool currently so fundamental to our daily lives, It&#39;s shocking how many people(software engineers even) cannot in plain terms explain what the internet is and how it works. I couldn&#39;t for the longest time because I was so engrossed in the creation of websites that I forgot to consider the work it took to deliver these products to the end-user.</p><p>What does an understanding of the workings of the internet bequeath you? Asides from bragging rights and an appreciation for the wonder that it is, knowing how the internet works will enable you (as a developer) to optimise your code in order to speed up the delivery of web pages. Sounds intriguing enough? if so, let&#39;s crack on 😁.</p><h3>What is the internet?</h3><p>The internet is a worldwide network that transmits data and media across interconnected devices.</p><p>It began as an experiment named ARPANET in the 1960s and was geared toward building a communication system that could survive a nuclear attack(Ka-boom!!!).</p><p><strong><em>How does data get displayed on your device?</em></strong> I used to think it was mainly through satellites but if that was the case the latency would be unbearable for this impatient generation (yea Zoomers I’m talking to you 😂). To know how majority of devices display pages we must first understand how they get internet connections.</p><h3>How we get connected to the internet</h3><p>The internet gets to us through a network of optical fiber cables(information transferred with light rays) laid underneath the sea between several data-centers (buildings containing powerful computers called servers dedicated to sending out data when requested) and your device(client) location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*SGtj_hQcCWfqpIQD.jpg" /><figcaption>The earth’s submarine fiber optic cable network</figcaption></figure><p>When you connect to a router via wifi or ethernet cables, we get linked up to this network of optical fibre cables through our ISP — internet service providers(those guys that swap your money for browsing credit).</p><h3>Step 1 — Requesting a web page</h3><p>When you enter a URL(Uniform Resource Locator) into your browser, the browser seeks to obtain the IP Address for the specified domain name.</p><blockquote>An <strong>IP address</strong> is a number assigned to every device connected to a network that uses the Internet protocol. They come in the form <strong>nnn.nnn.nnn.nnn</strong> where nnn must be a number from 0–255.</blockquote><p>Every device(server, computer, or router) that is connected to the internet is identified uniquely by a string of numbers known as an IP Address. It acts like a <em>shipping address</em> through which all information reaches its destination on the internet.</p><blockquote>The Internet Corporation for Assigned Names and Numbers(<strong>ICANN</strong>) is an institution that manages IP address assignment and domain name registration.</blockquote><h3>Step 2 — Resolving domain names to IP Addresses.</h3><p>You can access a website by knowing the IP address of the server it is situated in, but because they are difficult to remember, domain names like google.com are used which correspond to IP Addresses(currently 216.58.223.238).</p><p>The internet uses a huge phone book-like service called <strong>DNS</strong>(domain name system) to keep track of computer names and their corresponding IP addresses on the Internet with the aid of distributed databases(DNS databases).</p><p>Many computers called DNS servers connected to the Internet host a subset of the DNS database. Through an ISP your browser first connects to your primary DNS server specified as part of the internet connection setup on your device. In the case where a DNS server fails to find the IP address for a domain, it re-directs the requesting computer to another DNS server till the domain is resolved.</p><h3>Step 3 — Sending request and receiving response using HTTP</h3><p>Once an IP address is obtained the browser forwards a Hypertext transfer protocol(HTTP) request to the target server through your ISP.</p><blockquote>HTTP is the language used for internet communication. HTTPS is a secure version of HTTP where communication between browser and website are encrypted.</blockquote><p>When the server gets a request to access a particular website. The data is sent back in a huge collection of ones and zeros from the server in form of light pulses via the optical fibre cables. During the journey, the response gradually peels through the onion layer of all the IP addresses the request came through in order to find its way back to the exact machine that initiated the process.</p><h3>Step 4 — Reassembling Data Packets</h3><p>What makes the medium so efficient is that the data is broken into tiny sequenced pieces called packets before it is transferred. Packets might not be routed through the same paths as they would take the best route available at the time of their transfer. Upon reaching your device the packets are reassembled sequentially in accordance with the Internet protocol(IP) and Transport control protocol(TCP).</p><blockquote>The IP directs packets to a specific computer using an IP address while TCP directs packets to a specific application on a computer using a port number.</blockquote><p>After the packets are pieced together a web page or resource is birthed and ready for consumption by the user. Browsers have become so fast at doing this you most times don’t even realize it happening. amazing right? 🤯</p><h3>Conclusion</h3><p>Phew!! that was a lot. A number of details were skipped in order to provide a high-level abstraction of the internet and make it bite-sized enough to digest. Now you understand how data moves through the internet, you are ready to grasp the intricate details involved in rendering a web page on a browser and more.</p><blockquote>I’m glad you took the time to read my blog. If you liked it give me a <strong>clap</strong> and follow my medium account for more content like this. Thanks<em> 👋.</em></blockquote><h3>References</h3><p><a href="https://web.stanford.edu/class/msande91si/www-spr04/readings/week1/InternetWhitepaper.htm">How Does the Internet work?</a></p><p><a href="https://www.youtube.com/watch?v=Dxcc6ycZ73M&amp;list=PLzdnOPI1iJNfMRZm5DDxco3UdsFegvuB7">What is the internet?</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/How_the_Web_works">How does the web work — Learn web development | MDN</a></p><p><a href="https://www.youtube.com/watch?v=7_LPdttKXPc">How does the Internet work in 5 minutes</a></p><p><a href="https://www.youtube.com/watch?v=x3c1ih2NJEg">How does the Internet work? | ICT</a></p><p><a href="https://www.hp.com/us-en/shop/tech-takes/how-does-the-internet-work#:~:text=It%20works%20by%20using%20a,where%20you&#39;re%20using%20it">How does the internet work | HP Tech takes</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56b184316d8d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/frontend-mastery-how-the-internet-works-56b184316d8d">Frontend Mastery — How the internet works</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: Events]]></title>
            <link>https://medium.com/geekculture/javascript-101-events-9c90fb7e2dce?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c90fb7e2dce</guid>
            <category><![CDATA[events]]></category>
            <category><![CDATA[event-capturing]]></category>
            <category><![CDATA[event-bubbling]]></category>
            <category><![CDATA[javascript-101]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Tue, 14 Dec 2021 21:02:18 GMT</pubDate>
            <atom:updated>2021-12-20T10:07:58.666Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/899/0*2UwS3xn5Q9sfaEx3.jpg" /></figure><p>An <em>event</em> is a signal that something has happened and is modeled after the <strong>observer pattern</strong> which allows loose coupling between the behavior and appearance of the page.</p><p>Events, after they occur, are propagated throughout the DOM for their listeners to respond to. The order in which these events are received on the page is described as the <strong>Event flow.</strong></p><h3>DOM Event Flow</h3><p>The event flow specified by <a href="https://www.w3.org/TR/WD-DOM-Level-2/events.html#Events-Event">DOM Level 2 Events</a> has three phases: the <strong>event capturing </strong>phase, at the target, and the <strong>event bubbling</strong> phase. Event capturing occurs first, providing the opportunity to intercept events if necessary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vMV3rcv-imNcbVZMg7_EnQ.png" /><figcaption><a href="https://codepen.io/caleb-42/pen/ExwPbQM">Even capturing and bubbling flow after <strong>div</strong> is clicked</a></figcaption></figure><p>In the DOM Level 2 Events specification event flow, the actual target (the &lt;div&gt; element) does not receive the event during the <strong>capturing phase</strong>. The diagram above depicts otherwise because <strong>Safari</strong>, <strong>Chrome,</strong> and <strong>Firefox</strong> fire an event during the capturing phase at the event target(and the example was done in chrome).</p><p>This means that the capturing phase which would have stopped at the parent of event target (&lt;body&gt; element) continues to the event target (&lt;div&gt; element) before the next phase from the event target begins(<strong>bubbling phase)</strong>, which fires on the &lt;div&gt; to travel back up to the document.</p><blockquote>The end result of this modification is that there are two opportunities to work with the event on the target.</blockquote><h3>Event Capturing</h3><p>The theory of event capturing(aka <strong>event trickling</strong>) is that the least specific node should receive the event first and the most specific node should receive the event last. To use event capturing the third argument(<strong>useCapture </strong>in this case) of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener</a> method is given the value <strong>true</strong> like so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TRtaKhYC_eHt0fDc6Bp38Q.png" /><figcaption>Event capturing flow after <strong>div</strong> is clicked</figcaption></figure><p>With event capturing, the click event is first received by the document and then continues down the DOM tree to the actual target of the event, the &lt;div&gt; element.</p><blockquote>Event capturing is generally not used because of a lack of support in older browsers. The general advice is to use event bubbling freely while retaining event capturing for special circumstances.</blockquote><h3>Event Bubbling</h3><p>Event bubbling is when an event is said to have started at the most specific element (the deepest possible point in the document tree) and then flows upward toward the least specific node (the document). To use event bubbling the third argument(<strong>useCapture </strong>in this case) of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener</a> method is given the value <strong>false</strong>. But since false is its default value, we would ignore it like so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iMaEG7nFrkQUBP1y1yuY_A.png" /><figcaption>Event bubbling after <strong>div</strong> is clicked</figcaption></figure><p>When you click the div element the click event occurs in the following order: div — body — html — document. The click event goes up the DOM tree firing on each node along its way until it reaches the document object. (firefox, chrome, and safari continue till the window object) — crazy right? they always do extra 😂.</p><h3>Event Delegation</h3><p>Capturing and bubbling allow us to implement one of the most powerful event-handling patterns called event <strong>delegation</strong>. It is also possible to use the target property to cast a wide net for a specific type of event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4kv848AYtuBINKoDHi_FfQ.png" /><figcaption><a href="https://codepen.io/caleb-42/pen/RwLrzrb">Event delegation in practice</a></figcaption></figure><p>For example, if you have a node containing a long list of buttons like the above image, it may be more convenient to register a single click handler on the outer node(<strong>div</strong> in this case) and have it use the target property to figure out whether a button was clicked, rather than register individual handlers on all of the buttons.</p><h3>More About Events</h3><h4>Stopping Event Propagation</h4><p>At any point, an event handler can call the <strong>stopPropagation</strong> method on the event object to prevent handlers further up from receiving the event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p2MwZ17PDw-agK1OiksWOA.png" /><figcaption><a href="https://codepen.io/caleb-42/pen/mdBVZVd"><strong>stopPropagation</strong> method in action</a></figcaption></figure><p>This can be useful when, for example, you have a button inside another clickable element and you don’t want clicks on the button to activate the outer element’s click behavior.</p><h4>Prevent Default Event</h4><p>Many events have a default action associated with them. A common example is the refresh action that occurs when you submit a form. For most types of events, the JavaScript event handlers are called before the default behavior takes place. If the handler doesn’t want this normal behavior to happen, typically because it has already taken care of handling the event, it can call the preventDefault method on the event object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZluH7FmWOMgNgFLru6J_ig.png" /><figcaption><a href="https://codepen.io/caleb-42/pen/dyVGxxZ"><strong>preventDefault</strong> Event method in action</a></figcaption></figure><p>This can be used to implement your own keyboard shortcuts or context menu. It can also be used to obnoxiously interfere with the behavior that users expect.</p><h3>Conclusion</h3><p>Without events and listeners our programs though pretty would be nothing more than an expression of ideas and emotions, void of feedback. This is what makes events an interesting topic in Javascript as they enable us to get a sense of how the world feels about our apps via interaction. I hope you’ve learned a thing or two about how they work and how to take advantage of their propagation throughout your app. See ya 👋</p><h3>References</h3><ul><li><a href="https://www.amazon.ca/Eloquent-JavaScript-3rd-Introduction-Programming/dp/1593279507">Eloquent Javascript</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c90fb7e2dce" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-events-9c90fb7e2dce">Javascript 101: Events</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: Web workers]]></title>
            <link>https://medium.com/geekculture/javascript-101-web-workers-1c70bcb4ba90?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c70bcb4ba90</guid>
            <category><![CDATA[shared-worker]]></category>
            <category><![CDATA[javascript-101]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-workers-in-javascript]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Mon, 06 Dec 2021 10:41:41 GMT</pubDate>
            <atom:updated>2021-12-14T21:05:10.148Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*-O8v_7CkKtvce79ClyRJTg.png" /></figure><p>Javascript is a single-threaded language which means it can only act on one task at a time. To handle multitasking Javascript switches between tasks just like a single-core machine would do via a concept known as concurrency — <strong>virtual parallelism</strong>.</p><p>This model though safe for non-blocking tasks can become a nightmare for heavy resource-demanding programs(e.g audio compression and sound conversion on the client) that most times need to run at the same time(<strong>parallelism</strong>) and not just have overlapping time periods(<strong>concurrency</strong>) with the main application. This is where Web workers come in.</p><h3>What is a Web Worker?</h3><blockquote>A Web Worker is a feature of the browser (aka host environment) that provides <strong>multiple instances</strong> of the Javascript engine, allowing us to run different program in separate threads.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*Mh4BvzNU9EminMNHYDQ_GQ.png" /></figure><p>A web worker can be initialized as shown above and the browser will spin up a separate thread and let the worker file run as an independent program. This type of parallelism is called “task parallelism,” as the emphasis is on splitting up chunks of your program to run in parallel.</p><blockquote>Note — JavaScript still doesn’t support multi-threaded execution. But through browsers, web workers affords us the ability to.</blockquote><h3><strong>Worker Environments</strong></h3><p>The Worker does not have access to the page’s DOM but has a copy of the navigator, location, JSON, and applicationCache variables and can perform network operations (Ajax, Websockets) and set timer actions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*MpZ5bSVCf8khjl1azHpx8w.png" /></figure><p>The <strong>w1</strong> Worker object is an event listener and trigger, which lets you subscribe to events sent by the Worker as well as send events to the Worker as shown above;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*6PQRyKXwqa-Q6GF8Z1pKdA.png" /></figure><p>Meanwhile, in the worker file above, we listen for the message event and access the transmitted payload from the data property of the event argument. A combination of all the snippets we’ve seen will create a dedicated worker file that would output <em>“completed serious computation”</em> after 5000 milliseconds at which time the main application can carry out other activities without its single thread being blocked.</p><p>This example of web workers although not practical enough paints an image of how other heavier processes are best handled on the browser like; Processing intensive math calculations, Sorting large data sets, Data operations (compression, audio analysis, image pixel manipulations, etc.)</p><h3>Shared Workers</h3><p>Having a worker dedicated to the main application kind of gets clunky as the need for shared resources grows across browser tabs. And the thought of having them recreated for each browser instance of the same app seems redundant. That&#39;s where Shared workers come in.</p><blockquote>A shared worker is a kind of worker that can be accessed from several browsing context: windows, iframes or other workers</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*QyIE-KLOwm1zQQRBRA-1aQ.png" /></figure><p>Because a shared Worker can be connected to or from more than one program instance or page on your site, the Worker needs a way to know which program a message comes from. This unique identification is called a “port” — think network socket ports. So the calling program must use the port object of the Worker for communication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*0kY2nAbsKKWfE-bH7iHKHQ.png" /></figure><p>Inside the shared Worker, an extra event must be handled: “connect”. This event provides the port object for that particular connection. Other than these few nuances Shared and dedicated workers practically have the same implementation.</p><h3>Conclusion</h3><p>Web workers are an exciting technology which if used properly can give a sense of multi-threading in javascript in the browser. Its flexibility outside of the web page opens the door to features (Push notification, background sync, etc) that can be performed as a service and most times don’t need user interaction.</p><p>Next stop — <a href="https://caleb-codes.medium.com/javascript-101-events-9c90fb7e2dce">Events</a></p><h3>References</h3><ul><li><a href="https://www.amazon.ca/You-Dont-Know-JS-Performance/dp/1491904224">You Don’t Know JS: Async &amp; Performance</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c70bcb4ba90" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-web-workers-1c70bcb4ba90">Javascript 101: Web workers</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: Handling Asynchrony — Part 2]]></title>
            <link>https://medium.com/geekculture/javascript-101-handling-asynchrony-part-2-555bebbaf83e?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/555bebbaf83e</guid>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[javascript-101]]></category>
            <category><![CDATA[event-queue]]></category>
            <category><![CDATA[javascript-asynchrony]]></category>
            <category><![CDATA[job-queue]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Mon, 29 Nov 2021 10:18:14 GMT</pubDate>
            <atom:updated>2021-12-07T05:42:34.247Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript 101: Handling Asynchrony — Part 2</h3><p>An asynchronous call returns only after it has done its work and is capable of blocking the main thread of a program’s execution. Many languages handle this by starting an additional <strong>thread</strong>(<em>an independent path of execution</em>) for the request. JavaScript on the other hand uses an <strong>event queue </strong>to keep a mental note of the request’s execution while the <strong>main</strong>(<strong>single) thread</strong> keeps busy with other things.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PyDWXh2-RLoJrHx0.jpg" /></figure><h3>Event Loop</h3><p>When an asynchronous call happens there is often a call back associated that is eventually placed at the end of an <strong>event queue.</strong></p><blockquote>The “Event Loop” is the cycle the Javascript engine runs continuously through the event queue to execute each callback it meets till the queue is empty.</blockquote><p>The event queue serves as a to-do list for the thread while the event loop is the thread continuously checking its to-do list till it&#39;s empty. If the queue is empty the thread goes to sleep but when a new event comes up on the queue the loop begins its cycle again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*s-ZoPFlbLw2bL-DBsOEENA.png" /></figure><p>The above is a vastly simplified version of the event loop. As you can see, there’s a continuously running loop(event loop) represented by the while loop, and each iteration of this loop is called a <strong>“tick.”</strong> For each tick, if an event is waiting in the queue, it’s taken off and executed. These events are your function callbacks.</p><blockquote>An important note is that <strong>ES6</strong> now specifies how the event loop works, which means technically it’s within the purview of the <strong>JS engine</strong>, rather than just the <strong>hosting environment. </strong>One main reason for this change is the introduction of <strong>ES6 Promises</strong>, because they require the ability to have direct, fine-grained control over scheduling operations on the event loop queue</blockquote><h3><strong>SetTimeOut — behind the scenes</strong></h3><p>Seeing how asynchronous events operate in JS we understand that the setTimeout(..) doesn’t put your callback on the event loop queue. What it does is set up a timer; when the timer expires, the JS engine places your callback into the event loop, such that some future tick will pick it up and execute it.</p><p><em>What if there are already 20 items in the event loop at that moment</em>? Your callback waits. It gets in line behind the others — there’s not normally a path for preempting the queue and skipping ahead in line. This explains why setTimeout(..) timers may not fire with perfect temporal accuracy. You’re guaranteed that your callback won’t fire before the time interval you specify, but it can happen at or after that time, depending on the state of the event queue. <strong>Amazing right? 😊</strong></p><h3><strong>Job Queue</strong></h3><p>As of ES6, there’s a new concept layered on top of the event loop queue, called the “Job queue.” The most likely exposure you’ll have to it is with the asynchronous behavior of Promises.</p><blockquote>the “Job queue” is a queue hanging off the end of every tick in the event loop queue.</blockquote><p>Certain async-implied actions that may occur during a tick of the event loop will not cause a whole new event to be added to the event loop queue, but will instead add an item (aka Job) to the end of the current tick’s Job queue.</p><p>A Job can also cause more Jobs to be added to the end of the same queue. So, it’s theoretically possible that a Job “loop” (a Job that keeps adding another Job, etc.) could spin indefinitely, thus starving the program of the ability to move on to the next event loop tick.</p><p>Let’s imagine an API for scheduling Jobs (directly, without hacks), and call it schedule(..). Consider</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*N568j0GvjCYly05ZWG8QJQ.png" /></figure><p>You might expect this to print out A B C D, but instead, it would print out A C D B because the Jobs happen at the end of the current event loop tick, and the timer fires to schedule for the next event loop tick (if available!)</p><h3>Conclusion</h3><p>Javascript is single-threaded and handles asynchronous processes with its event loop model to schedule callbacks that need to be executed at a later time. Applications built on top of node.js use the least threads possible to reduce resource usage and complexity as compared to multi-threaded programming. That is a big plus.</p><p>Next stop — <a href="https://medium.com/geekculture/javascript-101-web-workers-1c70bcb4ba90">Web workers</a></p><h3>References</h3><ul><li><a href="https://www.amazon.ca/You-Dont-Know-JS-Performance/dp/1491904224">You Don’t Know JS: Async &amp; Performance</a></li><li><a href="https://www.amazon.ca/Eloquent-JavaScript-3rd-Introduction-Programming/dp/1593279507/ref=pd_lpo_2?pd_rd_i=1593279507&amp;psc=1">Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=555bebbaf83e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-handling-asynchrony-part-2-555bebbaf83e">Javascript 101: Handling Asynchrony — Part 2</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: Handling Asynchrony — Part 1]]></title>
            <link>https://medium.com/geekculture/javascript-101-handling-asynchrony-part-1-7e944ee6c76e?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e944ee6c76e</guid>
            <category><![CDATA[javascript-asynchrony]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript-101]]></category>
            <category><![CDATA[callback]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Mon, 22 Nov 2021 13:08:17 GMT</pubDate>
            <atom:updated>2021-12-07T05:40:28.078Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript 101: Handling Asynchrony — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/0*7Ghr-r5C16htdsG-.jpg" /></figure><p>When programs interact with things outside of the processor like communicating over a computer network, the operating system switches the processor between multiple running programs so it does not sit Idle. This is a classic case of asynchrony. Javascript being single-threaded has a number of ways of dealing with Asynchrony. Let&#39;s get to know them 😊.</p><h3>Callbacks</h3><p>Callbacks are the fundamental unit of asynchrony in JS. In this approach to asynchronous programming, a function that performs a slow action takes an extra argument, a <strong>callback function</strong>. The action is started, and when it finishes, the callback function is called with the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*TSGbbzAs5C2RdGqGEO8VJw.png" /></figure><p>In the example above, the setTimeout function, available both in Node.js and in browsers, waits a given number of milliseconds (a second is a thousand milliseconds) and then calls a function(Callback).</p><p>This style of programming is easy to implement, but the indentation level increases with each asynchronous action and leads to a dilemma referred to as <strong>Callback Hell.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*uLa82k1J9KsW7CWbmGB2eA.png" /></figure><h3>Why Callbacks aren’t so great</h3><p>First, our brains plan things out in sequential, blocking, single-threaded semantic ways, but callbacks express asynchronous flow in a rather nonlinear, which makes reasoning properly about such code much harder.</p><p>Second, and more importantly, callbacks suffer from <strong>inversion of control</strong> in that they implicitly give control over to another party (often a third-party utility not in your control!) to invoke the continuation of your program. This control transfer leads us to a troubling list of trust issues, such as whether the callback is called more times than we expect.</p><h3>Promises — A better approach</h3><p>Instead of arranging for a function to be called at some point in the future, we can return an object that represents this future event.</p><blockquote>A promise is an asynchronous action that may complete at some point and produce a value. It is able to notify anyone who is interested when its value is available.</blockquote><p>To create a promise, you can use Promise as a constructor. It has a somewhat odd interface — the constructor expects a function as an argument, which expects two functions — resolve(for success path) and reject(for error path) during its invocation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*DdgBG42PUGLHN5cceACTeQ.png" /></figure><p>What we see above is a <strong>promise chain</strong> that expresses async flow in a sequential fashion. To get the result of a promise, you can use its then method. This registers a callback function to be called when the promise resolves and produces a value. But that’s not all the <strong>then</strong> method does. It returns another promise, which resolves to the value that the handler function returns. The catch method is used to handle errors.</p><h3>ES7 Async and Await</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*B75ll1nHk2QXMcUR.jpeg" /></figure><p>A more recent addition to the JavaScript language is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async functions</a> and the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">await</a> keyword, part of the so-called ECMAScript 2017 JavaScript edition. These features basically act as syntactic sugar on top of promises, making asynchronous code easier to write and to read afterward</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*C3PJbhq_hVIlI1IskfEP5Q.png" /></figure><p>This is what calling the CallEndpoint from our previous examples would look like if we incorporated async and await. The async keyword, which you put in front of a function declaration turns it into an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async function</a>. An async function returns a Promise by default and expects the possibility of the await keyword being used to invoke asynchronous code</p><p>The main advantage of promises is that they simplify the use of asynchronous functions. Instead of having to pass around callbacks, promise-based functions look similar to regular ones: they take input as arguments and return their output. The only difference is that the output may not be available yet.</p><h3>Conclusion</h3><p>It’s been fun right? we’ve seen the most common implementation of asynchrony in JS via callbacks and have examined its drawbacks. Promises however offer a better programming interface and the closest resemblance to normal functions in javascript using <strong>async/await</strong>. Promises don&#39;t get rid of callbacks, they just redirect the orchestration of those callbacks to a trustable intermediary mechanism that sits between us and another utility. Promises are awesome. Use them!</p><p>Next stop — <a href="https://medium.com/geekculture/javascript-101-handling-asynchrony-part-2-555bebbaf83e">Handling Asynchrony — Part 2</a></p><h3>References</h3><ul><li><a href="https://www.amazon.ca/You-Dont-Know-JS-Performance/dp/1491904224">You Don’t Know JS: Async &amp; Performance</a></li><li><a href="https://www.amazon.ca/Eloquent-JavaScript-3rd-Introduction-Programming/dp/1593279507/ref=pd_lpo_2?pd_rd_i=1593279507&amp;psc=1">Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e944ee6c76e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-handling-asynchrony-part-1-7e944ee6c76e">Javascript 101: Handling Asynchrony — Part 1</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: Objects]]></title>
            <link>https://medium.com/geekculture/javascript-101-objects-1d071851a01a?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d071851a01a</guid>
            <category><![CDATA[javascript-object]]></category>
            <category><![CDATA[objects]]></category>
            <category><![CDATA[javascript-101]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Mon, 15 Nov 2021 15:44:23 GMT</pubDate>
            <atom:updated>2021-12-13T08:02:18.464Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6IgCUw9Os7-vV_4a.jpg" /></figure><p>Today we start by debunking a common misconception among Developers(and Spartans alike). Objects indeed are the general building block upon which much(not all) of JS is built. They do make up the eigth primary types in Javascript along with <strong>string, number, boolean, null, undefined, bigint, and Symbols.</strong> So to say that everything in Javascript stems from objects is clearly untrue. Apologies <strong>King Leonidas 😂</strong>.</p><h3>But what are Objects really?</h3><blockquote>An object is a collection of properties, and a property is an association between a name (or <em>key</em>) and a value</blockquote><p>In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. In the same way, JavaScript objects can have properties, which define their characteristics.</p><p>Objects in javascript come in two forms; declarative(literal) and constructed form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*-TIeNmH4gF6P6oum5_zmmw.png" /></figure><p>The only difference really is that you can add one or more key/value pairs to the literal declaration, whereas with constructed-form objects, you must add the properties one by one.</p><blockquote>In objects, property names are always strings. If you use any other value besides a string (primitive) as the property, it will first be converted to a string.</blockquote><h3>Computed Property Names</h3><p>ES6 adds computed property names, where you can specify an expression, surrounded by a [ ] pair, in the key-name position of an object-literal declaration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*LTrDaaJdWbWhGCiosdpcOg.png" /></figure><p>The perks of this feature come to light when the aim is to use a variable&#39;s value as an object’s key. This is a true testament to javascript&#39;s dynamism.</p><h3>Property Descriptor</h3><p>We can use Object.defineProperty(..) to add a new property or modify an existing one (if it’s configurable!), with the desired characteristics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U9mR6sDdBci4u8PrFzBNeQ.png" /></figure><ol><li><strong>Writable</strong><br>The ability for you to change the value of a property is controlled by writable.</li><li><strong>Enumerable</strong><br>The name probably makes it obvious, but this characteristic controls whether a property will show up in certain object-property enumerations, such as the for..in loop.</li><li><strong>Configurable<br></strong>As long as a property is currently configurable, we can modify its descriptor definition, using the same defineProperty(..) utility: There’s a nuanced exception to be aware of: even if the property is already configurable(false), writable can always be changed from true to false without error, but not back to true if already false. Another thing configurable(false) prevents is the ability to use the <strong>delete operator</strong> to remove an existing property.</li></ol><h3>Object Immutability</h3><p>Sometimes you want to make properties or objects that cannot be<br>changed (either by accident or intentionally). Although there are a number of ways to achieve this, It’s important to note that all of these approaches create shallow immutability. That is, they affect only the object and its direct property characteristics. Below are the ways in JS to achieve immutability</p><ol><li><strong>Object constant</strong><br>By combining writable: false and configurable: false, you can essentially create a constant (cannot be changed, redefined, or deleted) as an object property</li><li><strong>Prevent Extention</strong><br>If you want to prevent an object from having new properties added to it, but otherwise leave the rest of the object’s properties alone, call Object.preventExtensions(..).</li><li><strong>Seal</strong><br>Object.seal(..) creates a “sealed” object, which means it takes an existing object and essentially calls Object.preventExtensions(..) on it, but also marks all its existing properties as configurable: false.</li><li><strong>Freeze<br></strong>Object.freeze(..) creates a frozen object, which means it takes an existing object and essentially calls Object.seal(..) on it, but it also marks all “data accessor” properties as writable: false, so that their values cannot be changed.</li></ol><h3>Getters and Setters</h3><p>Getters are properties that actually call a hidden function to retrieve a value. Setters are properties that actually call a hidden function to set a value. When you define a property to have either a getter or a setter or both, its definition becomes an “accessor descriptor” (as opposed to a “data descriptor”). For accessor descriptors, the value and writable characteristics of the descriptor are moot and ignored, and instead, JS considers the set and get characteristics of the property (as well as configurable and enumerable).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C_6fAuwTXei26H0p2yEnkw.png" /></figure><p>we created a property on the object that actually doesn’t hold a value, but whose access automatically results in a hidden function call to the getter<br>function, with whatever value it returns being the result of the property access. Since we only defined a getter for <strong>a</strong>, if we try to set the value of <strong>a</strong> later,<br>the set operation won’t throw an error but will just silently throw the<br>assignment away. Below is a complete getter and setter scenario which you will most likely encounter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g-ShIhQ6lslh9PEErwzV9w.png" /></figure><h3>Conclusion</h3><p>With this, we’ve come to the end of our introduction to Objects in Javascript. Objects are a critical piece and do have a lot of actions that can be carried out on them to make application creation easier in Javascript. But there is more…. In future lessons, we’ll delve deeper and examine <strong>prototypes</strong> and <strong>behavior delegation</strong> in order to fully grasp the power that lies within this seemingly primitive type. Catch ya later 👋.</p><p>PS — If you think the connection of Spartans was irrelevant to this topic… You are absolutely right 😂. But it sure broke the ice, didn&#39;t it?</p><p>Next stop — <a href="https://medium.com/geekculture/javascript-101-handling-asynchrony-part-1-7e944ee6c76e">Handling Asynchrony — Part 1</a></p><h3>References</h3><p><strong>You Don’t Know JS: this &amp; Object Prototypes </strong>—<a href="https://www.amazon.com/You-Dont-Know-JS-Prototypes/dp/1491904151"> https://www.amazon.com/You-Dont-Know-JS-Prototypes/dp/1491904151</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d071851a01a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-objects-1d071851a01a">Javascript 101: Objects</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript 101: What is “this”]]></title>
            <link>https://medium.com/geekculture/javascript-101-what-is-this-eaeb4a740a9b?source=rss-8bf526ce4814------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaeb4a740a9b</guid>
            <category><![CDATA[this]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-101]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ewere Ebie]]></dc:creator>
            <pubDate>Wed, 10 Nov 2021 07:09:48 GMT</pubDate>
            <atom:updated>2021-12-13T08:04:29.054Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/570/0*Fpej3mUJR4jblnXC.jpg" /></figure><p>If there has ever been a more tricky subject in Javascript, “<strong>this”</strong> would be it(see what I did there 😏). I can’t count the number of times I’ve had to guess the “<strong>this” binding</strong> for a particular function to make sense of a coding challenge all to no avail. As elusive as it may be, understanding it is sure to bring more clarity. Here is a simple overview;</p><blockquote>The <strong>THIS</strong> reference in javascript is a <strong>runtime</strong> binding and is based on the conditions of its enclosing function’s invocation or call.</blockquote><h3>Confused? Let&#39;s go deeper.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*5yUB8Z12BI9agD3szD7YrQ.png" /></figure><p>When a function is invoked, an activation record, otherwise known as an execution context is created. This record contains information about;</p><ul><li>Where the function was called from (Call-stack)</li><li>How the function was invoked</li><li>And what parameters were passed. etc</li></ul><p>One of the properties of the record is the “<strong>This”</strong> reference. Function <strong>foo’s </strong>call-site is in the <strong>global object</strong> and hence binds to it when “<strong>this”</strong> is referenced within <strong>foo</strong>. Since <strong>variable a</strong> is defined in the global object as 2, a call to foo would check the global object for <strong>a</strong> and print out 2 to the console. This method of resolving a “this” reference is called <strong>Default binding</strong> and is the most common case of function calls.</p><h3>Resolving “THIS” in an Executing Function</h3><p>Now to the Bone of Contention. Default binding is the most common(weakest) of Four ways to determine the “this” binding for a particular function. Below are the other methods to do so.</p><ol><li><strong>Implicit Binding</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*Q82oGdpijlhnBswzGrCjuA.png" /></figure><p>Here the call-site uses the obj context to reference the function. The obj owns the function reference at the time the function is called. A call to foo emits 2 by looking up the value of <strong>a</strong> in its <strong>“this”</strong> binding which is currently is obj.</p><blockquote>One common frustration with this binding type is that a this reference can be <strong>implicitly lost</strong> through function callbacks. In the case of events in javascript, the this context is directly overridden to point to the DOM instance the event was called against.</blockquote><p>2. <strong>Explicit Binding</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*sJ4MouVd8FzrWP-4SChwfg.png" /></figure><p>Invoking foo with explicit binding by foo.call(..) allows us to force its “this” to be obj. A downside to this method is that it can only be enforced at the call site which sometimes is not in our control like in the case of callbacks where a “this” context can get lost or replaced. To circumvent this hindrance we apply a stricter variation of explicit binding called <strong>hard binding</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*WXPR9jQP9K_VKURfG_bJJw.png" /></figure><p>In hard binding we create a function bar() which, internally, manually calls foo.call(obj), thereby forcibly invoking foo with obj binding for this. No matter how you later invoke the function bar, it will always manually invoke foo with obj. This binding is both explicit and strong.</p><blockquote>Since hard binding is such a common pattern, it’s provided with a built-in utility as of ES5 — Function.prototype.bind. And is used like so:<strong> foo.bind(obj)</strong></blockquote><p>3.<strong> New Operator</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*uh5AaXdGnQkcL-LzdGbMwg.png" /></figure><p>When a function is invoked with new in front of it, otherwise known as a constructor call, the following things are done automatically:</p><ul><li>A brand new object is created (aka constructed) out of thin air.</li><li>The newly constructed object is [[Prototype]]-linked.</li><li>The newly constructed object is set as the “this” binding for that function call.</li></ul><p>Unless the function returns its own alternate object, the new-invoked function call will automatically return the newly constructed object which serves as the “this” binding for the function.</p><h3>Lexical This</h3><p>In situations where identifying a “this” reference could prove to be a hassle, ES6 does give us a way to escape the guessing game entirely. Instead of the four standard binding rules, ES6 <strong>arrow-functions</strong> use lexical scoping for “this” binding, which means they inherit the “this” binding (whatever it is) from its enclosing function call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*MQtuoKwdjQW-4TK-TUF51g.png" /></figure><blockquote>It’s important to note that arrow-functions are essentially disabling the traditional <strong>this mechanism</strong> in favor of more widely understood <strong>lexical scoping.</strong> by providing an alternative to using bind(..) on a function.</blockquote><h3><strong>Conclusion</strong></h3><p>That was enlightening right? Not only have we understood “THIS” we’ve also learned how to use arrow functions to make use of the enclosing scope’s “this” via lexical scoping. I hope you never find yourself guessing what “this” is anymore. <strong>Ciao 👋</strong></p><p>Next stop — <a href="https://medium.com/geekculture/javascript-101-prototypes-9c06739635b0">Prototypes</a></p><h3>References</h3><p><strong>You Don’t Know JS: this &amp; Object Prototypes </strong>—<a href="https://www.amazon.com/You-Dont-Know-JS-Prototypes/dp/1491904151"> https://www.amazon.com/You-Dont-Know-JS-Prototypes/dp/1491904151</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaeb4a740a9b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/javascript-101-what-is-this-eaeb4a740a9b">Javascript 101: What is “this”</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>