<?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 Aaron Tribou on Medium]]></title>
        <description><![CDATA[Stories by Aaron Tribou on Medium]]></description>
        <link>https://medium.com/@tribou?source=rss-b7c0fc6834e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tzCjic0fhz4yxcfR.jpeg</url>
            <title>Stories by Aaron Tribou on Medium</title>
            <link>https://medium.com/@tribou?source=rss-b7c0fc6834e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:06:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tribou/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[React and Flux: Migrating to ES6 with Babel and ESLint]]></title>
            <link>https://medium.com/@tribou/react-and-flux-migrating-to-es6-with-babel-and-eslint-6390cf4fd878?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/6390cf4fd878</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[flux]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Thu, 15 Oct 2015 03:57:22 GMT</pubDate>
            <atom:updated>2017-08-30T01:58:53.800Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ugyHdaQXGqkon18fFcZS6Q.png" /></figure><p><em>*Update: The following guide has now been updated with the latest Babel v6 features.</em></p><p><strong>As</strong> <strong>of June 2015</strong>, the <a href="http://www.ecma-international.org/ecma-262/6.0/">ECMAScript 2015</a> (ES6) standard has been ratified for the world of JavaScript. Consequently, it’s time to get familiar with the <a href="https://babeljs.io/docs/learn-es2015/">new JavaScript syntax</a> and use it in new projects. For this task, I’ll take my existing <a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea">React/Flux Todo App</a> and convert the front-end React and Flux components from ES5 to ES6.</p><p>Since the ES6 standard was ratified only a few months prior to the writing of this article, many JavaScript engines don’t support all the features yet. However, I can use <a href="https://babeljs.io/">Babel</a> to compile the ES6 features down to ES5 automatically in my existing Webpack asset pipeline. For the actual syntax information, I can use the <a href="https://github.com/airbnb/javascript">Airbnb JavaScript Style Guide</a> for reference and add their <a href="https://www.npmjs.com/package/eslint-config-airbnb">ESLint configuration plugin</a> to assist the detection of ES5 migration opportunities.</p><p>Some highlights of the new syntax are <a href="https://github.com/airbnb/javascript#10.1"><em>import</em>/<em>export</em> for Modules</a>, <a href="https://github.com/airbnb/javascript#references"><em>const</em>/<em>let</em> instead of <em>var</em></a>, <a href="https://github.com/airbnb/javascript#9.1">native classes</a>, <a href="https://github.com/airbnb/javascript#arrow-functions">arrow functions</a>, and <a href="https://github.com/airbnb/javascript#6.4">string templates</a>. Since this is a React project, I’ll include and follow the <a href="https://github.com/airbnb/javascript/blob/master/react/README.md">React/JSX Style Guide</a> as well.</p><h4>Improving the Webpack Pipeline</h4><p>For the first step, I need to add Babel, ESLint, and the ESLint configurations with the following <em>npm</em> packages. For completeness, I’ll also include the normal React/Flux/Hapi/Webpack dependencies <a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea">from my previous post</a>:</p><pre># Install runtime dependencies<br>npm install --save react react-dom flux hapi hapi-react-views inert vision dateformat superagent babel-register babel-preset-es2015 babel-preset-react</pre><pre># Install build time dependencies<br>npm install --save-dev webpack babel-core babel-loader babel-eslint eslint eslint-loader eslint-plugin-react eslint-config-airbnb browser-sync browser-sync-webpack-plugin</pre><p>With those dependencies in place, I can add Babel and ESLint to my existing <em>webpack.config.js</em> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b899771eba22cd7a13f644f2859ed76/href">https://medium.com/media/8b899771eba22cd7a13f644f2859ed76/href</a></iframe><p>First, I added a <em>preLoaders</em> section to ensure the linting occurs before Babel transpiles the code. There I specified that the <em>eslint-loader</em> should look for both <em>.jsx</em> and <em>.js</em> files, only look in the <em>assets</em> directory, and exclude the <em>bundle.js</em> file generated by Babel. Next in the <em>loaders</em> section, I edited the <em>test</em> property regex to include both <em>.js</em> and <em>.jsx</em> files. I also replaced <em>jsx-loader</em> with <em>babel-loader</em> since Babel converts JSX automatically, and I excluded the <em>node_modules</em> folder for faster rendering. <em>For an explanation of Webpack and the other settings, see my previous posts about </em><a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea"><em>serving React and Flux with Hapi and Webpack</em></a><em> and </em><a href="https://medium.com/@tribou/adding-browsersync-to-hapi-and-webpack-c9e4a04c4539?source=your-stories"><em>Adding BrowserSync to Hapi and Webpack</em></a><em>.</em></p><p>To finish the ESLint setup, I added this small <em>.eslintrc</em> configuration file to the root of the project, which simply includes the Airbnb configuration I installed earlier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4930783834a0f8eca0b5ed89b26e964/href">https://medium.com/media/f4930783834a0f8eca0b5ed89b26e964/href</a></iframe><p>However, since I use Vim for editing and like to have an extra newline at the end of files, here’s an example of how you can override the ESLint configuration with your own rules:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/198cd7c6b797d1d2ae09560d39e7c1bf/href">https://medium.com/media/198cd7c6b797d1d2ae09560d39e7c1bf/href</a></iframe><p>Any rules explicitly stated in the <em>.eslintrc</em> in the root of your project will override any existing configurations you are extending.</p><p>Next, I need to make sure I have this <em>.babelrc</em> file at the root of my project which is required with Babel v6+:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ec26a4e87ab922f60d4f6e05b868e01/href">https://medium.com/media/5ec26a4e87ab922f60d4f6e05b868e01/href</a></iframe><p>As an optional step, I’m also going to edit my <em>package.json</em> scripts to add a lint script that mimics the <em>eslint-loader</em> settings in my Webpack config. This gives either myself or a build pipeline the option to run ESLint by itself:</p><pre>“lint”: “eslint --ignore-pattern **/bundle-*.js --ext .js,.jsx assets/**”</pre><p>At this point, I now have the tools in place to start using ES6 features.</p><h4>Realizing the Future of JavaScript</h4><p>With the new Webpack setup, I can use my previous npm development script to kick off the watching, transpiling, bundling, and linting all at once:</p><pre>npm run dev</pre><p>Or I can just run the linting as a one-off task with the following:</p><pre>npm run lint</pre><p>Either way, I’m immediately met with a slough of linting issues. In the next sections, I’ll walk through some of the more popular changes that can be expected with ES6.</p><p><strong>Exchanging <em>var</em> for <em>const</em> and <em>let</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*2q6zSPc1U_eKo40psvX_gA.png" /></figure><p>Immutability is highly encouraged in ES6 to help prevent side effects from changing variables, which can be the source of difficult debugging issues in any programming language. Therefore, I am charged to think about each variable declaration as either a static <em>const</em> or a mutable <em>let</em>. Perhaps the most popular error in my initial linting is this <em>var</em> issue returned as “unexpected var, use let or const instead.” Since the first variable errors in most of my files are require statements, I can use the new ES6 <em>import</em> format to prevent the variable mutation:</p><pre>import React from ‘react’;<br>import { removeItem } from ‘../actions/TodoActions.js’;</pre><p>The meaning of the first line can be directly compared to using <em>require</em>. On the second line, I’m specifying the import of an individual function that I’ll later export from my <em>TodoActions</em>.</p><p>For non-module situations, I’ll have to identify if the variable needs to change or not and use <em>const</em> or <em>let</em> <a href="https://github.com/airbnb/javascript#variables">appropriately</a>. However, there is also a special situation with Flux worth mentioning. Later in this post, the ES6 version of my Flux store uses <em>let</em> and ESLint throws an error stating “`_store` is never modified, use `const` instead.” In this case, I know my Flux store will need to mutate, so I can’t use <em>const</em>. Instead, I can turn to Facebook’s <a href="https://facebook.github.io/immutable-js/">Immutable.js</a> module to create my store (perhaps in another post).</p><p><strong>Using JavaScript classes</strong></p><p>Another lint error involving <em>var</em> is using <em>React.createClass()</em> to create React components. The ES6 way of resolving this issue is to switch to the <em>class extends</em> format. In this format, the <em>var</em> statement is replaced by an <em>export default class </em>which<em> extends </em>the React <em>Component </em>class. Just like the legacy <em>modules.export </em>in ES5, the <em>export </em>keyword allows other files to import this class in ES6. Finally, the <em>default</em> means that I can omit the curly braces when importing it from another file.</p><p>In addition, the standard JavaScript object notation format is gone. Instead, I just list my <em>_destroy</em> and <em>render</em> functions without commas or semicolons. The ES6 and React style guides also tell me to move the render function to the bottom, rearrange some whitespace, add <a href="https://github.com/airbnb/javascript#19.2">trailing commas</a>, separate each component into its own file, and add React props validation. Consequently, here is a before and after of my <em>TodoItem</em> component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c9b6ddd0203d3734194329c3d302c89/href">https://medium.com/media/1c9b6ddd0203d3734194329c3d302c89/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb5a993d680d8299b351aac9015fb031/href">https://medium.com/media/eb5a993d680d8299b351aac9015fb031/href</a></iframe><p>Another change to note is the <em>constructor</em> function introduced by ES6. With an official class structure in place, the React team decided to remove the “magical” <a href="https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding">autobinding feature</a> of <em>React.createClass</em> and exchange it for allowing developers to bind their own class functions. For instance, the <em>_delete</em> function above will not have access to <em>this.props.index</em> until it is bound to the class inside the <em>constructor</em> with the following line:</p><pre>this._delete = this._delete.bind(this);</pre><p>Although this decision could cause some headaches for developers making the transition (as it did for me), it provides consistency overall for projects using both React and non-React classes.</p><p><strong>Launching arrow functions</strong></p><p>Perhaps one of the most anticipated ES6 features is the arrow function. Taking cues from CoffeeScript, ES6 finally acknowledges this shorthand as first class JavaScript. One good opportunity to use this feature arrives when encountering a “missing function expression name”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*wusNFbvXR1xBZe-D7B0KTw.png" /></figure><p>However, that can also mean I am exporting a function with the legacy <em>module.exports</em> format and need to switch to the new import/export modular form. The <em>RandomUserAPI</em> module in my project provides both function format opportunities:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5a3c76e42d14af1faef5bfb185a5bf/href">https://medium.com/media/9c5a3c76e42d14af1faef5bfb185a5bf/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efbc004ab054566ee712017e5e48edc8/href">https://medium.com/media/efbc004ab054566ee712017e5e48edc8/href</a></iframe><p>In the above example, I’m first converting a legacy exported function into an ES6 named function. Second, I’m exchanging the anonymous callback that handles the end of the Random User API request with an arrow function. In addition, I’m choosing to leave the <em>console</em> statement even though ESLint throws a warning. Ideally in a large project, I would exchange console usage with a dedicated client-side logging module that could forward logs to a server module like <a href="https://github.com/winstonjs/winston">Winston</a> or <a href="https://github.com/trentm/node-bunyan">Bunyan</a>.</p><p><strong>Building string templates</strong></p><p>Another very welcome enhancement to the JavaScript language is the string template. This allows me to reflect on my PowerShell roots and embed variables inside the string statement without the frequent terminating and concatenating. In this project, the only file that has an existing example to use is the <em>TodoStore</em> which showcases the one-line string template (far) below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71f9ae24d063fc0d11973fd348119d21/href">https://medium.com/media/71f9ae24d063fc0d11973fd348119d21/href</a></iframe><p>The <em>let</em> variable above that defines the <em>_store</em> is, as I mentioned earlier, a good opportunity for Immutable.js. For now, though, I’ll leave it as it is. However, I can switch the mutative <em>splice</em> function in the <em>REMOVE_ITEM</em> section to a functional <em>filter</em>, which replaces the <em>_store</em> instead of mutating it.</p><p>An additional and especially important aspect of using the new JavaScript classes with Flux is implementing the singleton. Since I’ve converted the <em>TodoStore</em> from a JavaScript <em>prototype</em> assignment to the new ES6 class, I have to initialize the class as a singleton before registering it with the dispatcher and exporting it for use in React components. This is also true of the dispatcher which I converted to a class and instantiated in a singleton:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5fe7197998fc0c80b2c9ce92456ff21/href">https://medium.com/media/a5fe7197998fc0c80b2c9ce92456ff21/href</a></iframe><p><strong>A batteries-included component</strong></p><p>To finish things up, I wanted to showcase a React component that contains many of the post-ES6 migration features that could be encountered in a typical project. Here are some of the highlights:</p><ul><li>The <em>constructor</em> passes in the React props as a parameter,</li><li>A <em>super</em> keyword enables the use of the parent <em>React.Component</em> class methods,</li><li>Each class method is bound to the class so that it can be used within the other class methods, and</li><li>The initial state of the component is declared as a <em>state</em> property instead of using the legacy <em>getInitialState</em> function.</li></ul><p>Regarding the absence of the popular <em>getInitialState</em> React function, the <a href="https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#es6-classes">React team decided</a> to replace it with the native class instance property defined inside the <em>constructor</em>. Although <em>getInitialState</em> is gone, the <em>setState</em> method is still used normally as it is inherited from the <em>React.Component</em> class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f118e3be5939d8ca275f5a34f0906bdb/href">https://medium.com/media/f118e3be5939d8ca275f5a34f0906bdb/href</a></iframe><h4>Conclusion</h4><p>Similar to my <a href="https://medium.com/@tribou/react-and-flux-for-the-rest-of-us-61f90869d51f">introduction to the world of Flux</a>, forcing myself to transform a JavaScript project into ES6 provides a new level of understanding. Since JavaScript has been around for two decades, it’s a good investment to conquer the ES6 learning curve early as the changes will probably endure for many more years.</p><p>The full todo app from this post can be <a href="https://github.com/tribou/react-todo/tree/1.5.2">found on GitHub</a>.</p><p>For an example React/Redux app in ES6, check out my <a href="https://github.com/tribou/react-template">React Template</a>.</p><p><strong>If you found this post helpful, please feel free to share it!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6390cf4fd878" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Kubernetes on Google Cloud]]></title>
            <link>https://medium.com/google-cloud/getting-started-with-google-cloud-55937aa6b9?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/55937aa6b9</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Fri, 25 Sep 2015 00:59:54 GMT</pubDate>
            <atom:updated>2015-10-18T00:26:37.622Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*afkK1deEdJkuN3IhnzuzKg.png" /></figure><h4>Installing and Authenticating</h4><p>First, create a <a href="https://cloud.google.com/">Google Cloud</a> account, initialize a billing account, and add a new project. Next, install the assets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdc2182e6cbfa664cb576b11f3f707b6/href">https://medium.com/media/cdc2182e6cbfa664cb576b11f3f707b6/href</a></iframe><h4>Creating a 1-Node Kubernetes Cluster for $6/month</h4><p>Now you can create a new Kubernetes cluster on a <em>f1-micro</em> instance for a little over $6/month.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0094f5fc2134cf47c728e980351ad67e/href">https://medium.com/media/0094f5fc2134cf47c728e980351ad67e/href</a></iframe><p>Note that a <em>f1-micro</em> single-node cluster doesn’t allow cloud-logging or cloud-monitoring. However, you now can check out your new cluster in the <a href="https://console.developers.google.com/">Google Developers Console</a></p><p><em>*Important Note: if you create a Kubernetes service for a pod, it will automatically create a network forwarding rule on Google Cloud which is a </em><strong><em>minimum of $18/month</em></strong><em> for the first five rules (as of October 2015).</em></p><p>For next steps, check out:</p><p><a href="https://cloud.google.com/container-engine/docs/tutorials/hello-node">Deploying a Node.js Application</a></p><p><a href="https://cloud.google.com/container-engine/docs/tutorials/persistent-disk/">Deploying Apps with Persistent Disks</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55937aa6b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/getting-started-with-google-cloud-55937aa6b9">Getting Started with Kubernetes on Google Cloud</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding BrowserSync to Hapi and Webpack]]></title>
            <link>https://medium.com/@tribou/adding-browsersync-to-hapi-and-webpack-c9e4a04c4539?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9e4a04c4539</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[hapi]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Sun, 02 Aug 2015 17:38:27 GMT</pubDate>
            <atom:updated>2015-09-19T01:02:38.054Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Previously, I wrote about serving a <a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea">sample React and Flux todo app with Hapi and Webpack</a>. Yesterday, thanks to the social nature of GitHub and the programming prowess of <a href="https://github.com/kevinold">Kevin Old</a>, I ran across <a href="http://www.browsersync.io/">BrowserSync</a>. After finding the right configuration option, I was blown away and needed to share how simple it is to add auto-refreshing across multiple devices and browsers to my existing development workflow:</p><pre>var BrowserSyncPlugin = require(&#39;browser-sync-webpack-plugin&#39;);<br>...<br>plugins: [<br>  new BrowserSyncPlugin({<br>    proxy: &#39;localhost:8000&#39;<br>  })<br>]</pre><p>It was that simple.</p><h4>Saving Clicks and Refreshes</h4><p>Assuming I have the same Hapi and Webpack setup as my previous post, I’ll run the following <em>npm install</em> command to add the <em>browser-sync-webpack-plugin</em> module:</p><pre>npm install --save-dev browser-sync-webpack-plugin</pre><p>Next, I’ll edit my <em>webpack.config.js</em> file to include the plugin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fa775743b061939538c337d4b5f440d/href">https://medium.com/media/3fa775743b061939538c337d4b5f440d/href</a></iframe><p>In the first line, I simply required the plugin. At the end, I added the <em>BrowserSyncPlugin</em> to the plugins Webpack option. This included the <em>proxy</em> option which tells BrowserSync to act as a proxy to my regular localhost:8000 port and <a href="http://www.browsersync.io/docs/#requirements">inject an async script</a> just inside the <em>body</em> tag proxying all requests to a new port at <a href="http://localhost:3000/">http://localhost:3000</a> (default).</p><p>Once I edit the Webpack configuration, I’m done. Seriously. Now I can run start my development like I setup previously, and BrowserSync takes care of the rest:</p><pre>npm run dev</pre><p>When the app starts, BrowserSync tells me I automatically have local and external URLs available and opens my default browser to the local URL. I’ll open up a couple more browsers to the local URL; and since I’m connected to my home WiFi, I’ll open up my mobile phone’s browser to the external URL. With BrowserSync in place, adding new todo items on one browser instantly reflects on all the other browsers as well. In addition, if I edit one of my React or Flux assets, all of the browsers automatically refresh after Webpack completes the new bundle. Plus, if I edit one of my server assets, nodemon still restarts the server in the background which makes any new routes available while still keeping all open browsers connected via BrowserSync.</p><p>Want to force all browsers to refresh? BrowserSync also provides an administrative UI URL on port 3001 on both internal and external URLs. With this admin page, I can control many of the synchronized options including “Reload All” which will force all browsers to refresh.</p><h4>Finding Bugs</h4><p>By adding BrowserSync to my workflow, I instantly noticed a couple bugs with my app (which was a good thing). First, I was inadvertently making my Bootstrap table scrollable with a <em>table-responsive</em> class. Removing that wrapper <em>div</em> altogether fixed that issue.</p><p>I also noticed that while my adding tasks with my Random button worked across browsers, manually adding tasks did not. However, the Random button appears to initiate separate API calls for each browser. If I added a centralized server database with each browser receiving pushed updates for Todo items through WebSockets, it would most likely resolve this issue (although at that point, if I had three browsers open, I should expect to see three new entries show up if I push the Random button on one). Regardless, BrowserSync is a welcome addition to my development workflow.</p><p><a href="https://github.com/tribou/react-todo/tree/1.4.1">The full code for this example is on GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9e4a04c4539" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React and Flux: A Docker Development Workflow]]></title>
            <link>https://medium.com/@tribou/react-and-flux-a-docker-development-workflow-469957f3bbf0?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/469957f3bbf0</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[flux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Sun, 02 Aug 2015 02:39:33 GMT</pubDate>
            <atom:updated>2017-08-30T02:00:44.818Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p><a href="https://www.docker.com/">Docker</a> development environments are quickly becoming the <em>de facto standard </em>for productive programming. Perhaps <a href="https://www.vagrantup.com/">Vagrant</a> ignited the inspiration with it’s remarkable ability to share server environments across development workspaces. However, Docker’s simplistic <a href="https://docs.docker.com/reference/builder/">Dockerfiles</a> and fast build times including <a href="https://docs.docker.com/reference/builder/#from">inheritable build images</a> appear to be giving it a dominant position in the development ecosystem. In this post, I would like to create a Docker-powered <a href="http://facebook.github.io/react/">React</a> and <a href="https://facebook.github.io/flux/">Flux</a> development workflow for <a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea">my previous Todo app example</a>.</p><p>The challenge with this endeavor is finding a workflow that allows my React and Flux assets to be rapidly rebuilt with each iteration but still be served from inside the container to <a href="http://12factor.net/dev-prod-parity">make it as “prod-like” as possible</a>. Unfortunately, this can get very complex if these two goals are combined into one über Docker development environment. For instance, certain features like mounted volumes with symlinks or file-watching become less compatible across different operating systems such as Windows. Consequently, I’m breaking this workflow into two parts — the accelerated development and the container testing. An overview of the workflow would look like the following:</p><ol><li>Develop the app locally with <a href="http://nodemon.io/">nodemon</a> and <a href="http://webpack.github.io/">Webpack</a> for quick feedback to complete a feature.</li><li>Run the app and/or a test suite locally inside a Docker container for “prod-like” feedback.</li><li>Push a commit to a remote Git repository for automatic continuous integration (CI) tests, user acceptance tests, etc.</li></ol><p>The good news about this workflow is I can simply skip to step two if my workstation’s local environment does not support my application’s environment or if I don’t want to install all of my app’s dependencies locally. Since step two only requires Docker to be installed for any container-compatible development environment, I can switch among developing in Node.js, Ruby, Python, Go, Java, and more without having to install each environment locally. If you haven’t installed Docker, <a href="http://docs.docker.com/installation/mac/">head over here to get started</a>.</p><h4>The Docker Files</h4><p>In order to bring this example closer to a real-world scenario, I’m going to add both web and database containers. However, I’m only going to need a Dockerfile for my web container because the database container should be a standard, pre-built image usable by many applications. Consequently, I should be able to pull the database image directly from a public or private Docker registry. This helps to ensure that my application is not too tightly coupled with the database environment. For more information, see the <a href="http://12factor.net/backing-services">Backing Services</a> factor of a Twelve-Factor app.</p><p>The Dockerfile for the web container looks like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b70b17a9f0c97f99aa86152a9b462cc/href">https://medium.com/media/1b70b17a9f0c97f99aa86152a9b462cc/href</a></iframe><p>By having a Dockerfile in my code repository, it makes it easier for automated build tools to recreate the same container in a different environment. To quickly review the contents, I’m pulling from the <a href="https://registry.hub.docker.com/_/node/">official Node.js image on Docker Hub</a>, creating a web app directory, copying my local workspace to the container, installing dependencies, running build scripts, exposing the port, and starting the app. These steps allow me to create a build artifact which can be deployed locally or remotely to a CI server.</p><p>In addition, I’m going to add the following <em>.dockerignore</em> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0aa0a866ae7e54a4e776528a5e0e5a7/href">https://medium.com/media/d0aa0a866ae7e54a4e776528a5e0e5a7/href</a></iframe><p>Each line allows me to ignore one or more files or folders when Docker uploads my workspace to the container. Perhaps the most important line is the <em>node_modules</em> folder which ensures I don’t upload dependencies that were installed in my local environment. Especially if I was developing on a Windows workstation, uploading pre-built dependencies would reduce my dev-prod parity and increase the chance of having an environment-related bug getting deployed to QA or production.</p><h4>Step 1: The Local Workflow</h4><p>For local development with quick feedback, I’m going to run Webpack to watch my assets directory and rebuild my client-side assets bundle when those files change. I’m also going to run nodemon to watch my server assets and rebuild my docker container when those files change. This part is identical to the workflow <a href="https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea">specified in my previous post</a>. However, with Docker, I can now run a database container if needed as well. Here is a diagram to help visualize the different parts of this local workflow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*FT3k5q6fcIBjnrbwRk_TzA.png" /><figcaption>The local development workflow enables faster rebuilds of server and client assets.</figcaption></figure><p>Notice that if my app needed a database, I would need to use a <em>DB_HOST</em> environment variable to access the changing IP address across the local, CI, and production environments. To start up a MongoDB database container, I’ll run the following docker command to have it running in the background.</p><pre># Start a MongoDB database<br>docker run -d --name db mongo</pre><pre># Check that it&#39;s running<br>docker ps</pre><pre># Stop it when I&#39;m done<br>docker stop db</pre><pre># List all previously stopped containers<br>docker ps -a</pre><p>Next, I can export my <em>DB_HOST</em> variable (if needed) and run my NPM dev script to start my app which auto-restarts upon file changes:</p><pre># Export the usual Boot2Docker IP<br># Running `boot2docker ip` can double-check this<br>export DB_HOST=192.168.59.103</pre><pre># Start the app and file watching<br>npm run dev</pre><h4>Step 2: Running in a Container</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*LKQo-IMa2Pv7vUf_Dv8Zqw.png" /><figcaption>Building and testing an app in a container may take longer to get feedback, but it is more cross-compatible among operating systems.</figcaption></figure><p>Now on to the container-based development workflow, which should be cross-compatible as long as Docker can be installed. Since I’ve already created a Dockerfile, I only need to build an image and run a container using the image. Keep in mind, if I’m using Boot2Docker, the <em>docker</em> binary will connect to the Docker daemon on my locally running virtual machine and build the image there. However, I can also <a href="https://docs.docker.com/reference/api/docker_remote_api/">configure Docker to connect to a remote host</a> which will automatically upload my workspace to build and run on a target server. Either way, to build my image, I’ll run:</p><pre># Build the Dockerfile into a Docker image<br>docker build -t todo .</pre><pre># List all local images<br>docker images</pre><p>The <em>-t</em> flag assigns the repository name <em>todo</em> to my image. If I wanted to be more conventional, I could pass a repository name and tag such as <em>tribou/todo</em> to label my image. The final parameter is “.” which tells Docker to look in my local directory for the build context and a Dockerfile. Once the image is built, I can run it to test with the following command:</p><pre>docker run --rm --name web -it --link db:db -e DB_HOST=db -p 8000:8000 todo</pre><p>With this command, I’m auto-removing the container when it stops, naming it <em>web</em>, ensuring I can see output and pass commands, linking my database container, passing <em>db</em> as my <em>DB_HOST</em> variable, exposing my app’s port, and using my <em>todo</em> image to make the container. Since I’m linking my <em>db</em> container, Docker will automatically add a “db” alias entry with the appropriate IP address to the <em>/etc/hosts</em> file on my web app container. That’s why I don’t have to pass the actual IP address for the <em>db</em> container in this command.</p><p>At this point, I had to edit the <em>server.js</em> file to change the host property for the Hapi.js server from “<em>localhost</em>” to “<em>0.0.0.0</em>” to be able to access the web app externally on the Docker container. Afterwards, if my app starts up correctly, I should be able to access it at my Boot2Docker IP which makes the URL <a href="http://192.168.59.103:8000/">http://192.168.59.103:8000</a>. In addition, if I set up a test script such as <em>npm test</em>, I could pass that at the end of the previous <em>docker run</em> command to execute that as well. Running and testing the app with this method gives me a much higher probability that my app will work when deployed to a CI or production environment.</p><h4>Increasing Efficiency: Inheritable Builds</h4><p>Right now, it takes a minute or more for my Docker image to build. Fortunately, there are some things I can do to make Step 2 more efficient. First, I could use two build images instead of one. The first image could install dependencies; and the second could inherit the first, add the workspace again, and build and run the app. That way, I’m not installing dependencies over and over when testing the app. If I build the first image using the repository name <em>todo</em> like I did above, this could be the second Dockerfile which I’ll call <em>Dockerfile-dev</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7cb74b5f2e67662741ae6453f8f0587/href">https://medium.com/media/b7cb74b5f2e67662741ae6453f8f0587/href</a></iframe><p>To build this Dockerfile, I’ll have to pass the filename along with the normal <em>docker build</em> parameters:</p><pre>docker build -t dev -f Dockerfile-dev .</pre><p>Rebuilding this second Dockerfile is MUCH faster, which provides a more tolerable development experience. However, I now have to remember to rebuild the first <em>todo</em> image if I install any new dependencies during development.</p><h4>Increasing Efficiency: Docker Compose</h4><p>A second way to increase efficiency is taking advantage of <a href="https://docs.docker.com/compose/">Docker Compose</a> (not on Windows yet). Instead of having to remember or copy/paste complex <em>docker run</em> commands to recreate my Docker environment, Docker Compose (which comes with Docker on OSX/Linux) essentially stores all of the command-line arguments for creating one or more containers in a YAML file. For instance, I’m going to add this <em>docker-compose.yml</em> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f27d69c876ff909d6a690048ee7676bb/href">https://medium.com/media/f27d69c876ff909d6a690048ee7676bb/href</a></iframe><p>The above file will build my <em>web</em> container with the Dockerfile in the current directory, pass the <em>DB_HOST</em> variable, forward the specified ports, and link it with my <em>db</em> container, which it will build from the <em>mongo</em> image. This environment is then created with the following command:</p><pre>docker-compose up</pre><p>Using the second development Dockerfile with Docker Compose is as easy as adding the following line under the web container properties:</p><pre> dockerfile: Dockerfile-dev</pre><p>Notice: make sure you are using at least version 1.3 of Docker Compose to use the <em>dockerfile</em> property.</p><h4>Conclusion</h4><p>When developing an application, it’s one thing to get it running properly on a local workstation; but it’s quite another to get the app running in every required environment. Fortunately, Docker provides a method to remove much of the guesswork or configuration management skills needed to deploy an application to multiple environments including production. By taking the initiative to make an app <a href="http://12factor.net/">Twelve-Factor</a> or container-compatible, it will be much more accessible by a CI environment, an ops team, and the open source community.</p><p>A working example of this workflow can be <a href="https://github.com/tribou/react-todo/tree/1.3.0">found on GitHub</a>.</p><p><strong>If you found this post helpful, please feel free to share it!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=469957f3bbf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serving React and Flux with Hapi and Webpack]]></title>
            <link>https://medium.com/@tribou/serving-react-and-flux-with-hapi-and-webpack-213afacf94ea?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/213afacf94ea</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[hapi]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Mon, 20 Jul 2015 03:23:10 GMT</pubDate>
            <atom:updated>2015-12-25T17:13:42.836Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>In my previous two posts, I explained the process of <a href="https://medium.com/@tribou/react-and-flux-for-the-rest-of-us-61f90869d51f">connecting React to a Flux store</a> and <a href="https://medium.com/@tribou/flux-getting-data-from-an-api-b73b6478c015">getting external data from an API into Flux</a>. Using the <a href="https://github.com/facebook/flux/tree/8b5d160b78901eea92b62307cead92910e4a0389/examples">Facebook Flux examples</a>’ model of manually building an index.html file is great for learning, but now I want to serve my app according to a standard 12-Factor compliant model. This will allow me to publish my app to a server or Docker container for hosting. Since I’m familiar with <a href="http://hapijs.com/">Hapi.js</a>, I will use it as my HTTP server and <a href="http://webpack.github.io/">Webpack</a> to coordinate the packaging of my JavaScript assets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*HhGwYW0xvL__lRa6z7q4Qw.png" /><figcaption>A good visualization of this architecture strategy is provided by <a href="http://nerds.airbnb.com/isomorphic-javascript-future-web-apps/">Spike Brehm</a></figcaption></figure><p>One important note, it is becoming more and more popular to <a href="http://fluxible.io/guides/bringing-flux-to-the-server.html">render React and Flux on the server</a> to improve performance and user experience by serving “compiled” HTML and app state straight to the client. It’s also becoming popular to use <a href="https://venturebeat.com/2013/11/08/the-future-of-web-apps-is-ready-isomorphic-javascript/">isomorphic JavaScript</a> to organize and combine server-side and client-side codebases. While server-side Flux rendering and isomorphism is great, it adds a bit more complexity than I’d like to tackle right now in this todo example. Consequently, I’m going to choose a first-step strategy of rendering a React template on the server that delivers the SEO-compatible content as well as the state-managing React and Flux components to the client.</p><h4>Waking Up Hapi</h4><p>First, I’m going to initialize the app and install the Node.js dependencies. If you don’t have Node.js installed, <a href="https://nodejs.org/download/">head over here</a> to get that done.</p><pre># Initialize a new Node.js app<br>npm init</pre><pre># Install Hapi, React, Flux, and dependencies<br>npm install --save hapi react react-dom flux hapi-react-views vision inert object-assign dateformat superagent babel-register babel-preset-es2015 babel-preset-react</pre><p><em>*Update: Added babel to the list of dependencies since </em><a href="https://github.com/jedireza/hapi-react-views"><em>hapi-react-views</em></a><em> no longer uses </em>node-jsx<em> but requires you to register </em><a href="https://babeljs.io/docs/usage/require/"><em>Babel</em></a><em> in the </em>server.js<em> file. The upside to this is you can start using ES6 and ES7 features anywhere in the server-side code. For the client side, check out </em><a href="https://medium.com/@tribou/react-and-flux-migrating-to-es6-with-babel-and-eslint-6390cf4fd878"><em>Migrating to ES6 with Babel and ESLint</em></a><em>.</em></p><p><em>Also, Babel 6 requires a </em>.babelrc<em> file to be placed at the root of the project declaring the configuration:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ec26a4e87ab922f60d4f6e05b868e01/href">https://medium.com/media/5ec26a4e87ab922f60d4f6e05b868e01/href</a></iframe><p>Next, I’ll add a basic Hapi.js server file. This will provide a few things:</p><ul><li>An HTTP server accessible at <a href="http://localhost:8000/">http://localhost:8000</a> to serve the web app;</li><li>A view engine using the Hapi plugin <em>vision</em> and the <em>hapi-react-views</em> package to render the React/JSX components to a string before sending it to the client;</li><li>A static directory route using the Hapi plugin <em>inert</em> to serve client-side assets like CSS, JS, and IMG; and</li><li>A main app route that will serve a <em>Default.jsx</em> view template.</li></ul><p>Here’s the completed <em>server.js</em> file that I’ll put in my project’s root directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6d199fd393adc4fb6365450fa5186e3/href">https://medium.com/media/a6d199fd393adc4fb6365450fa5186e3/href</a></iframe><p>To start it up, I’ll simply run:</p><pre>node server</pre><h4>Adding a Default Template</h4><p>For the <em>Default.jsx</em> template that I’m placing in my <em>views</em> directory, I’ll create a new React component and copy over my existing <em>index.html</em> content. One important note, I had to <a href="https://facebook.github.io/react/docs/tags-and-attributes.html">alter some of the tags</a> when importing over to a JSX template. This included adding closing tags to the <em>meta</em> and <em>link</em> tags, changing the <em>charset</em> property to <em>charSet</em>, and <a href="https://facebook.github.io/react/tips/inline-styles.html">changing my inline style string</a> on the body tag to an embedded JavaScript object with camel-cased properties. Here is the completed template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a76cb6dc424d46cb9428dfdba256651/href">https://medium.com/media/0a76cb6dc424d46cb9428dfdba256651/href</a></iframe><p>Now I have the ability to dynamically set my script names, page title, and more with simple JavaScript if the need arises. In addition, I could embed any other components that need to be SEO-compliant; but for this example, I’m going to move on to better things.</p><h4>Building the Client Assets</h4><p>After setting up the server and template, it’s time to automate the bundling of the client-side assets. Webpack provides a holistic approach by automatically following an entry file to every dependency that it needs. It then bundles all of the dependencies into one file. In addition, I noticed Webpack bundles about twice as fast as Browserify (based only on my testing in this project).</p><p>Another item of interest is that Webpack is often used in conjunction with a <a href="http://gaearon.github.io/react-hot-loader/">“hot-loading” development environment</a> which automatically updates your code without refreshing the browser. In order to add hot-loading to this Hapi implementation, I’d need to take <a href="http://kevinold.com/2015/02/04/configure-webpack-dev-server-and-react-hot-loader-with-ruby-on-rails.html">Kevin Old’s approach</a> of attaching the webpack-dev-server to the public assets path that I’m serving. For now, I’ll resign to the fact that I’ll be refreshing my browser after changes like other web projects. <em>Update: </em><a href="https://medium.com/@tribou/adding-browsersync-to-hapi-and-webpack-c9e4a04c4539"><em>I recently added Webpack’s BrowserSync plugin</em></a><em> which can help boost productivity.</em></p><p>First, I’m going to move all my client-side assets into a new <em>assets</em> directory off the root. Second, I’ll install the development dependencies I’ll be using:</p><pre>npm install --save-dev nodemon webpack babel-core babel-loader</pre><p>I’ve added nodemon to the mix because it’s a simple way to watch files and restart the server with minimal configuration. By default, nodemon watches all files in the current directory and runs npm start. Since Webpack will be bundling and watching my client assets, I don’t need nodemon to restart the server when those change. Consequently, here’s the <em>nodemon.json</em> file I’ll place in the root directory to configure all this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7c44a2d2a2fe6f9b328d59033c51ca6/href">https://medium.com/media/f7c44a2d2a2fe6f9b328d59033c51ca6/href</a></iframe><p>The <em>restartable</em> option allows me to enter “rs” after nodemon is running to quickly have it restart (versus [Ctrl]+[C], [up], [enter]). The ignore array will have nodemon ignore the <em>.git</em>, <em>node_modules</em>, and <em>assets</em> folders.</p><p>Next, I’ll create the following <em>webpack.config.js</em> Webpack configuration file in the root directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/152b8638e1557738d6a9e996f1d8a2e9/href">https://medium.com/media/152b8638e1557738d6a9e996f1d8a2e9/href</a></iframe><p>The main parts of this file are the following:</p><ul><li>The <em>entry</em> — Webpack looks at the <em>Index.jsx</em> file to start bundling and tracing dependencies,</li><li>The <em>output</em> — defines the location of the bundled file, and</li><li>The <em>loaders</em> — it needs to process JSX file transformations with <em>babel-loader</em>.</li></ul><p>I also edited my <em>package.json</em> file to add the following script:</p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;node server.js&quot;,<br>  &quot;build&quot;: &quot;webpack -p --progress&quot;,<br>  &quot;dev&quot;: &quot;webpack --progress --color --watch &amp; nodemon&quot;<br>}</pre><p>This allows me to quickly build the bundle and start the server during development with the following command:</p><pre>npm run dev</pre><p>I can also kick of a bundle of a production version of my assets and start the server with these commands:</p><pre>npm run build<br>npm start</pre><p>Note that I won’t be able to simply run the <em>webpack</em> command directly since I opted to install it locally. However, using an NPM script automatically sources the <em>./node_modules/.bin</em> folder which makes the <em>webpack</em> command available in NPM scripts.</p><h4>Conclusion</h4><p>With Webpack and Hapi.js setup to bundle and serve, the Todo app can now be plugged into a deployment pipeline. By standardizing the app with a published port and providing build and run scripts, it’s even easier for an ops team or the continuous integration/delivery system to manage subsequent testing, packaging, and releasing. The next step might be <a href="https://medium.com/@tribou/react-and-flux-a-docker-development-workflow-469957f3bbf0">configuring and packaging the app in a docker container</a> for local development, remote testing, and deployment.</p><p>The full code of this example of serving React and Flux with Hapi and Webpack can be found <a href="https://github.com/tribou/react-todo/tree/1.5.2">here on GitHub</a>.</p><p><strong>If you found this post helpful, please let me know by selecting the heart below to recommend it.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=213afacf94ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous Flux — Getting Data from an API]]></title>
            <link>https://medium.com/@tribou/flux-getting-data-from-an-api-b73b6478c015?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b73b6478c015</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[flux]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Thu, 09 Jul 2015 02:21:44 GMT</pubDate>
            <atom:updated>2017-08-30T01:56:24.971Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AaHhQ83d8EN2gew1AejkxQ.png" /></figure><p>Once you understand <a href="https://medium.com/@tribou/react-and-flux-for-the-rest-of-us-61f90869d51f">the basics of the React/Flux relationship</a>, it’s time to add asynchronous API management. This will enable the data collection needed for larger web applications through a standard pattern. In the previous post, I walked through a Todo app example in order to conceptualize the process of connecting Flux to a React view. In this post, I will add an API call to bring external data into the app. I will be following the example outlined by Facebook in their <a href="https://github.com/facebook/flux/tree/master/examples/flux-chat">Flux Chat</a> app.</p><p>To conceptualize, it helps to remember that the premise behind the Action-Dispatcher-Store model is one-way data flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*K6EPDYWJj7mVRb9a_X9pqA.png" /></figure><p>This data flow is controlled by calling actions to initialize state changes which flow through the dispatcher to the stores. This means that user events in a view and the return of an API call are both updating the application state by calling an action to start the update process. Consequently, in this example, it makes sense to add actions for the start and the return of my API call. I’ll also abstract the logic of the API into a utility file instead of handling it directly in a view or action.</p><h4>Create New Constants</h4><p>First, I need to define the new constants that will represent the state changes for my API call. Since I want to keep this simple and focus on the architecture, I’ll just have one API call with the following two new constants: GET_RANDOM and GET_RANDOM_RESPONSE. I arbitrarily chose to use the <a href="https://randomuser.me/documentation">Random User Generator</a> as my API because it’s a simple data source and doesn’t require any signup or authentication.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99fc981688439abd823400d974aae5a2/href">https://medium.com/media/99fc981688439abd823400d974aae5a2/href</a></iframe><h4>Define the Actions</h4><p>Second, I need to define my new actions. The first action will be the clicking of the “Get Random” button which initiates the API call. The second action will be handling the return of the API call. I’m going to follow the convention of separating the API return action into a separate server<strong> </strong>actions file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba0941d455e8bbb2e8ef3dfd2a8d8854/href">https://medium.com/media/ba0941d455e8bbb2e8ef3dfd2a8d8854/href</a></iframe><p>In addition, I’ll edit the existing TodoActions.js file to add the API call action. This view action will include a function call to my RandomUserAPI, which I haven’t created yet. Keep in mind, I could very easily add a parameter to pass data through the getRandom() action if I needed to send data to the API. Since this is a simple example that doesn’t require any data passed to the API, the call looks pretty bare:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a4bbbdf1d3306593816695bb155e3c5/href">https://medium.com/media/0a4bbbdf1d3306593816695bb155e3c5/href</a></iframe><h4>Add the API Utility</h4><p>Third, I want to be a good coder and separate my concerns. Therefore, I’m going to create a separate utility file to hold all of my random user API-specific logic. I’m also going to use <a href="https://github.com/visionmedia/superagent">superagent</a> as my HTTP utility.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5a3c76e42d14af1faef5bfb185a5bf/href">https://medium.com/media/9c5a3c76e42d14af1faef5bfb185a5bf/href</a></iframe><h4>Add the Data to the Store</h4><p>Now that I have the actions defined, I need to add the GET_RANDOM_RESPONSE action case to my TodoStore.js to have it update the store when the API call returns. I randomly chose some fields from the Random User API result to put as the task text which was easily pushed to the existing list array representing my todo list state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1199bfdba3aef1941e76bd5448f5656/href">https://medium.com/media/a1199bfdba3aef1941e76bd5448f5656/href</a></iframe><h4>Add the Action to the View</h4><p>Finally, I need to add a “Random” button to the view to initiate this entire process. Since I don’t have a fancy mock-up to follow, I’ll just add a duplicate button underneath the existing one. In addition, I’ll change the onClick event handler to call the new getRandom() action I created earlier. The ease by which this new action can be implemented is a great example of the power and simplicity of using and maintaining React for your views.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acf1be7a751b882ef391d4255c4bc128/href">https://medium.com/media/acf1be7a751b882ef391d4255c4bc128/href</a></iframe><p>You can see the <a href="https://github.com/tribou/react-todo/tree/1.1.0">full app on GitHub</a>.</p><h4>Conclusion</h4><p>Once you have a Flux architecture in place, it becomes very straightforward to add external API calls to your web application. The points to remember are documenting your asynchronous send and receive events with constants, adding actions for each constant, separating external API logic for easy maintenance, updating the stores with the new state, and calling your actions directly from your views/components. Hopefully, this brief overview has helped you better understand how to manage external data in a Flux/React application architecture.</p><p><strong>If you found this post helpful, please feel free to share it!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b73b6478c015" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flux: Getting Past the Learning Curve]]></title>
            <link>https://medium.com/@tribou/react-and-flux-for-the-rest-of-us-61f90869d51f?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/61f90869d51f</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[flux]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Sun, 28 Jun 2015 14:59:00 GMT</pubDate>
            <atom:updated>2015-12-04T01:17:23.141Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>If you’re like me, you’ve probably run across the multitude of React and Flux conversations across the Internets and thought it would be a good skill to pursue. Perhaps, like me, you’ve also been able to follow along with the <a href="https://facebook.github.io/react/docs/thinking-in-react.html">Thinking in React tutorial</a> that helps you understand how to create a basic React app. Maybe you’ve even started a React app and finished some basic functionality.</p><p>Then you get about seven React components and three handlers manipulating the state, and you start to understand where <a href="https://facebook.github.io/flux/docs/overview.html#content">Flux</a> comes in. Your app, although small, starts to become unwieldy as you try to manage the increasing amounts of application state passing through the various React components. This must be where Flux comes in.</p><p>Where do you start? Perusing through the many Flux tutorials brings a quick realization that the next step in your application’s architecture is not so simple. This isn’t like learning another NPM package to add to your app. This is like learning MVC all over again. There are some frameworks like <a href="http://reapp.io/">Reapp</a> that could be used as a shortcut to complete your app, but it’s probably better to get a foundational understanding of what Flux is doing first. Consequently, I am writing this article to take myself step-by-step through the development of a Flux-based React application.</p><h3>Starting with a Mock</h3><p>Starting this process from the beginning means understanding the problem domain completely. Therefore, I’m going to start with a mock using <a href="https://marvelapp.com/">Marvel</a>. If you’re an advanced programmer, perhaps you can skip this step; but for me, I need to ensure I can visualize all the actions of the application before I try to conquer Flux.</p><p>After a few minutes of creative drawing on the mobile app, I ended up with the following Marvel App:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fmarvelapp.com%2F23ed51g%3Femb%3D1&amp;url=https%3A%2F%2Fmarvelapp.com%2F23ed51g&amp;image=https%3A%2F%2Fmarvelapp-live.storage.googleapis.com%2Fserve%2F1435502630.39_6822850.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=marvelapp" width="640" height="960" frameborder="0" scrolling="no"><a href="https://medium.com/media/f569fb549a202ebe57c9c0a1c4f6b4c6/href">https://medium.com/media/f569fb549a202ebe57c9c0a1c4f6b4c6/href</a></iframe><p>Beautiful. Now I have the visualization for some actions and views needed to complete this app. Before I get to the code, I’m going to continue my attempts to be a responsible programmer by writing down a basic outline of the app to understand the React and Flux pieces I’ll need.</p><p>To formally document what’s happening in this mock, here’s the outline:</p><ul><li>Click the “+” to add text for a new todo item</li><li>Click the “+” (or hit [enter]) beside the todo item to save it</li><li>Click the “+” to add text for a second todo item</li><li>Click the “+” (or hit [enter]) beside the second todo item to save it</li><li>Click the “x” beside the second todo item to remove it</li></ul><h3>Compose the Actions</h3><p>A quick glance at the <a href="https://facebook.github.io/flux/docs/overview.html#structure-and-data-flow">Flux data flow diagram</a> suggests that I should start the Flux architecture by defining my actions. Since the outline of my app is pretty basic, this step is easy:</p><ul><li>NEW_ITEM add a new todo item to the list for editing</li><li>SAVE_ITEM save the new item</li><li>REMOVE_ITEM remove a todo item from the list</li></ul><h4>Constants</h4><p>This is a good opportunity to create the TodoConstants.js file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6640b3e505a0044b4575552cf67e95c0/href">https://medium.com/media/6640b3e505a0044b4575552cf67e95c0/href</a></iframe><p>Since JavaScript <a href="http://stackoverflow.com/questions/130396/are-there-constants-in-javascript">doesn’t have solid support of constants yet</a>, creating mirrored key-pairs in all caps like this is the currently accepted convention in Flux.</p><h4>Dispatcher</h4><p>After I define my constants, do I jump to coding the Actions, the Stores, the Views, or the Dispatcher? In my mind, it makes sense in my mind to start with the files that have no dependencies and move out from there. This will lead me to the Dispatcher next.</p><p>When creating a dispatcher, a common convention is to create two handlers: one reacting to view triggered actions and one reacting to server triggered actions. I probably won’t have any server actions in this todo app, but I’ll add that part anyway for reference:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee9977458ee6ea72ea033a62cc7c38b3/href">https://medium.com/media/ee9977458ee6ea72ea033a62cc7c38b3/href</a></iframe><h4>Actions</h4><p>Now that I have my constants and dispatcher, I can use those to define the Action functions which will be used by the Views to manipulate application state. The convention for structuring the action object that gets returned is by having actionType and data properties. The composed actions for NEW_ITEM, SAVE_ITEM, and REMOVE_ITEM looks like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de0260169b94e1c597a3c44c774dbccf/href">https://medium.com/media/de0260169b94e1c597a3c44c774dbccf/href</a></iframe><h3>Create the Store</h3><p>The store, as the name implies, is where I keep my todo state that’s shared across multiple components. It’s also where you respond to applicable actions and change the appropriate shared state. In order to keep my todo app simple, the data will just be an array of string properties to represent todo items and an “editing” boolean field to track if a new item is in the process of being edited. With all that in mind, I <a href="http://tylermcginnis.com/reactjs-tutorial-pt-3-architecting-react-js-apps-with-flux/">searched around for a good Flux store example</a> and found this as the basic structure:</p><ul><li>Require the Dispatcher, Constants, and event emitter dependencies</li><li>Define the store as an empty array</li><li>Define “public” event listeners and getters that views will use to listen for changes and retrieve the store (read only)</li><li>Register the store with the dispatcher and handle each of the actions that will change the store’s data by using a switch statement and emitting a change event</li></ul><p>Putting all of this together looks like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2c88938d97408667cd58aaf4d92ff09/href">https://medium.com/media/d2c88938d97408667cd58aaf4d92ff09/href</a></iframe><h3>Construct the View</h3><p>Many React-Flux tutorials I encountered tackled the code for the view first. While that might be the natural process for experienced programmers, it was hard for me to wrap my head around all of the moving parts. By starting with the dependencies and scaffolding of the Flux architecture first, I now have the actions that I can use with my React components which makes more sense to me.</p><p>What’s the best way to compose the view? Well, since I already have a mock, it’s straightforward for me to use the <a href="https://facebook.github.io/react/docs/thinking-in-react.html">Thinking in React</a> approach to break down the view into components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*0eRZW2M58hQ0REHBG7yTbA.png" /></figure><p>I’m sometimes tempted to skip this step, but it’s highly recommended to make a basic sketch for reference. The more components that are added to a project, the more difficult it is to remember the hierarchy without a visual aid.</p><p>With these components sketched out, I can now translate those directly into React components. Since I use JSX, I like to begin with a static HTML page and copy over the components starting with the inner-most elements first. For a large project, I might separate each of the components into separate files; but for simplicity, I’ll keep it all in the same Index.jsx. First, here is my starting HTML page:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ded990620b371835f690ae9e5862311/href">https://medium.com/media/7ded990620b371835f690ae9e5862311/href</a></iframe><p>When breaking these components into JSX, I realized I should probably add an EditItem component as well. This component could be dynamically added to bottom of the TodoList if the “editing” property of the state is true. In addition, I found this good <a href="https://github.com/facebook/flux/blob/master/examples/flux-todomvc/js/components/TodoTextInput.react.js">flux-todomvc example</a> that gave me many of the input text properties. The “onChange” property suggested the idea to keep the value of the edited item in the EditItem component’s state for easy access when saving the text. Finally, I searched the <a href="http://getbootstrap.com/">Bootstrap</a> documentation and found the code for a little panel styling. In the end, these are the resulting React components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/743e241a8d539e21684ea0cbbf1551a2/href">https://medium.com/media/743e241a8d539e21684ea0cbbf1551a2/href</a></iframe><p>The completed app can be <a href="https://github.com/tribou/react-todo/tree/1.0.0">found on GitHub</a>.</p><h3>Conclusion</h3><p>On it’s own, React is a powerful templating framework as it’s very freeing to use the full power of JavaScript as a templating language. Coming from an MVC background, it feels simple and natural to combine your views and controllers into flexible components. With Flux added to the mix, I no longer have to pass handler functions up and down the component hierarchy in order to keep the application state under control. Although it’s a bit of a brain-crunch to get started with Flux, forcing myself to complete a simple app has gotten me past the learning curve and shown me the value.</p><p>Next steps? <a href="https://medium.com/@tribou/flux-getting-data-from-an-api-b73b6478c015">Perhaps handling the use of an API with Flux…</a></p><p>To cross-reference this guide with the new ES6 syntax, check out <a href="https://medium.com/@tribou/react-and-flux-migrating-to-es6-with-babel-and-eslint-6390cf4fd878">Migrating to ES6 with Babel and ESLint</a>.</p><h4>References</h4><p><a href="https://scotch.io/tutorials/getting-to-know-flux-the-react-js-architecture">Getting To Know Flux, the React.js Architecture</a></p><p><a href="http://tylermcginnis.com/reactjs-tutorial-pt-3-architecting-react-js-apps-with-flux/">React.js Tutorial Pt 3: Architecting React.js Apps with Flux</a></p><p><a href="https://facebook.github.io/react/docs/thinking-in-react.html">Thinking in React</a></p><p><a href="https://github.com/facebook/flux/tree/master/examples/flux-todomvc">Flux TodoMVC</a></p><p><strong>If you found this post helpful, please let me know by selecting the heart below to recommend it.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61f90869d51f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Road to Continuous Delivery]]></title>
            <link>https://medium.com/@tribou/the-road-to-continuous-delivery-ce6f770abf26?source=rss-b7c0fc6834e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce6f770abf26</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[twelve-factor]]></category>
            <dc:creator><![CDATA[Aaron Tribou]]></dc:creator>
            <pubDate>Sat, 13 Jun 2015 18:05:26 GMT</pubDate>
            <atom:updated>2015-10-15T01:19:43.115Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YO_jtliXGw65MuiqEhxjmA.png" /></figure><p>Continuous Delivery promises some incredible benefits. Simpler software deployments to multiple environments, standardized quality gates for multiple applications, and flexible change management across business departments are a few of the scintillating gems waiting to be unearthed. However, an advanced software assembly line will not be easily imposed on any organization, and understanding its dependencies is crucial to managing its stakeholders. This introduction describes some of the abstract obstacles an organization may need to overcome when pursuing Continuous Delivery.</p><h4>Standardized Applications</h4><p>Continuous Delivery requires that applications be built and deployed rapidly to multiple environments. Unfortunately, many applications are not designed to conform to this type of process. One dependency that enables Continuous Delivery is requiring the applications to be standardized.</p><p>A popular standardization used in continuously delivered applications is <a href="http://12factor.net/">The Twelve-Factor App</a>. Compiled by Heroku co-founder Adam Wiggins, the Twelve Factors are guidelines that can be used to keep a web application small, stateless, and scalable. The first factor, <a href="http://12factor.net/codebase">codebase</a>, is perhaps the most insightful because it draws the line between “black box” vendor applications and internal development. In the codebase factor, all of the code for the application must be stored in version control. Unfortunately, this automatically excludes any application for which one does not have the source code.</p><p>Are the Twelve Factors never applicable to a vendor’s “black box” application? Fortunately, having a team that understands the Twelve Factors will improve their decision-making in regards to “off-the-shelf” applications as well. Being able to understand the Twelve Factors and implement them into an application’s architecture provides insight that will help combat technical debt across many areas of the IT department. In addition, understanding the microservice architecture encouraged by the Twelve Factors provides opportunities to supplement existing vendor applications with independently deployable developed applications.</p><blockquote>Create and deploy a Twelve-Factor app for yourself to better understand the benefits this architecture can bring to a development team and an organization.</blockquote><h4>Standardized Infrastructure</h4><p>Deployment velocity cannot increase until the underlying infrastructure is standardized through continuous improvement and automation. Development teams can utilize the Scrum method of retrospectives to continuously find bottlenecks in their processes and implement small improvements.</p><p>For instance, if a team finds itself constantly fighting with discrepancies across development workstations, it may want to pursue using Docker to standardize the development environment. In regards to the Twelve Factors, this would also help to accomplish the principle of <a href="http://12factor.net/dev-prod-parity">Dev-Prod Parity</a>. Another example could be that a team struggles to update the production environment with updated code from version control on a regular basis because of deployment complexities. Using a tool like <a href="https://jenkins-ci.org/">Jenkins</a> or <a href="http://www.go.cd/">Go Continuous Delivery</a> could enable the team to break down the deployment process into standardized steps or a pipeline which could be automatically triggered after specific version control events. This improvement would help achieve the <a href="http://12factor.net/build-release-run">Build, Release, Run</a> factor. Continuous improvement cycles like these should perpetuate until deployments can take place in either hours or minutes.</p><p>A common fallacy is that the appropriate infrastructure must already be in place before an organization can even start down the road to Continuous Delivery. Rather, especially when using agile project management practices, Continuous Delivery will slowly pull or demand the infrastructure as the team progresses. Companies steeped in waterfall methodologies may find this to be one of the larger challenges primarily when trying to convince co-workers that Continuous Delivery is possible in such a traditional environment. Nevertheless, even the most traditional of managers can be swayed if the business value is identified.</p><blockquote>Do not assume that entirely new infrastructure needs to be built in order to practice Continuous Delivery. Start small, and practice continuous improvement.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce6f770abf26" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>