<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Matt Chaffe</title>
    <description>The latest articles on Forem by Matt Chaffe (@mattchewone).</description>
    <link>https://forem.com/mattchewone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55885%2Ffc8c0aff-465a-4005-8c5b-1aba70f3ef13.png</url>
      <title>Forem: Matt Chaffe</title>
      <link>https://forem.com/mattchewone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mattchewone"/>
    <language>en</language>
    <item>
      <title>FizzBuzz coding challenge</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Mon, 22 Jul 2019 09:00:00 +0000</pubDate>
      <link>https://forem.com/mattchewone/fizzbuzz-coding-challenge-2m48</link>
      <guid>https://forem.com/mattchewone/fizzbuzz-coding-challenge-2m48</guid>
      <description>&lt;p&gt;&lt;a href="/static/e21303dd85ec17c8c68319e1b27dbc4e/33840/image.jpg"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmattchaffe.uk%2Fstatic%2Fe21303dd85ec17c8c68319e1b27dbc4e%2Fb3ee8%2Fimage.jpg" alt="Laptop with code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whilst I was going through my backlog of videos to watch, I watched this &lt;a href="https://www.youtube.com/watch?v=QPZ0pIK_wsc" rel="noopener noreferrer"&gt;video&lt;/a&gt; which explains the use of a coding interview question; The FizzBuzz coding interview question. I wanted to have a go at answering the question myself and wanted to share my thought process as I did it.&lt;/p&gt;

&lt;p&gt;I stopped the video at around 1:40 as I wanted to go through an answer it myself without seeing the answer within the video.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  My solutions
&lt;/h2&gt;

&lt;p&gt;With the above question in mind, my first thought on how to approach this was how to determine the multiples of 3 and 5. What came to mind was the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()" rel="noopener noreferrer"&gt;modulus/remainder operator&lt;/a&gt; &lt;code&gt;%&lt;/code&gt; I knew that multiples of 3 &amp;amp; 5 should retain remainder 0, so this could be used to determine when to output the correct text.&lt;/p&gt;

&lt;p&gt;To start the challenge I used &lt;a href="https://jsfiddle.net/" rel="noopener noreferrer"&gt;jsfiddle&lt;/a&gt; and I created the following as my starting point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzbuzz (num) {
  return num
}

for (let i = 1; i &amp;lt;= 100; i++) {
  console.log(fizzbuzz(i))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then started on my implementation which first looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzbuzz (num) {
  if (num % 3 === 0 &amp;amp;&amp;amp; num % 5 === 0) {
    return `fizzbuzz`
  } else if (num % 3 === 0) {
    return `fizz`
  } else if (num % 5 === 0) {
    return `buzz`
  }

  return num
}

for (let i = 1; i &amp;lt;= 100; i++) {
  console.log(fizzbuzz(i))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First I check if the number is a multiple of 3 &amp;amp; 5 if so I return the combined &lt;code&gt;fizzbuzz&lt;/code&gt; text, I then check if the number is a multiple of either 3 or 5 and output the correct text, falling back to just returning the initial number.&lt;/p&gt;

&lt;p&gt;This is quite verbose and has a bit of duplication of the checks for the multiples, so I thought I would improve my answer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzbuzz (num) {
  const isFizz = num % 3 === 0
  const isBuzz = num % 5 === 0

  if (isFizz &amp;amp;&amp;amp; isBuzz) {
    return `fizzbuzz`
  } else if (isFizz) {
    return `fizz`
  } else if (isBuzz) {
    return `buzz`
  }

  return num
}

for (let i = 1; i &amp;lt;= 100; i++) {
  console.log(fizzbuzz(i))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, this is better but still a bit verbose, we have removed the duplicated checks into a single check that can be re-used, but still has the multiple if statement. How can this be reduced and made more concise I thought.&lt;/p&gt;

&lt;p&gt;Here is my final solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzbuzz (num) {
  const isFizz = num % 3 === 0
  const isBuzz = num % 5 === 0

  return `${isFizz ? 'fizz' : ''}${isBuzz ? 'buzz' : ''}` || num
}

for (let i = 1; i &amp;lt;= 100; i++) {
  console.log(fizzbuzz(i))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I removed the &lt;code&gt;if statement&lt;/code&gt; in favour of a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator" rel="noopener noreferrer"&gt;ternary&lt;/a&gt; within a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" rel="noopener noreferrer"&gt;template literal&lt;/a&gt; which uses a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators" rel="noopener noreferrer"&gt;short-circuit&lt;/a&gt; operator to fallback to the number. This boils down to creating a string which first checks if the number is a multiple of 3 which will then add the word &lt;code&gt;fizz&lt;/code&gt;, then checks if the number is a multiple of 5 which will add the word &lt;code&gt;buzz&lt;/code&gt;. Which will account for being either and both, and it will be an empty string if neither of them is truthy in this case the short-circuit operator will evaluate the empty string as falsey and will use the what is on the right hand of the operator which is the initial number.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JSCodeshift and ASTs</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Tue, 16 Jul 2019 09:00:00 +0000</pubDate>
      <link>https://forem.com/mattchewone/jscodeshift-and-asts-3ike</link>
      <guid>https://forem.com/mattchewone/jscodeshift-and-asts-3ike</guid>
      <description>&lt;p&gt;&lt;a href="/static/0115ca5ba07a8ac63c39f121c3314d36/e3340/tree.jpg"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmattchaffe.uk%2Fstatic%2F0115ca5ba07a8ac63c39f121c3314d36%2Fb3ee8%2Ftree.jpg" alt="Tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have been working on some open-source tools for &lt;a href="https://canjs.com" rel="noopener noreferrer"&gt;CanJS&lt;/a&gt;. I am working on the migration tool which is used to help upgrade projects to the latest version. We use &lt;a href="https://github.com/facebook/jscodeshift" rel="noopener noreferrer"&gt;jscodeshift&lt;/a&gt;to handle these migrations and this was my first time using them and writing them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jscodeshift&lt;/code&gt; allows you to create complex transformations that can read the source file and parse that into an &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;Abstract Syntax Tree (AST)&lt;/a&gt;. We can then manipulate it and regenerate the source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  AST Explorer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://astexplorer.net/" rel="noopener noreferrer"&gt;AST Explorer&lt;/a&gt; has been an &lt;strong&gt;extremely&lt;/strong&gt; useful tool in helping me understand AST’s and I’ve been using it to create my transforms live in the browser. It was very helpful to me to see the transformations happening as I was writing the transformation.&lt;/p&gt;

&lt;p&gt;To enable the &lt;code&gt;transforms&lt;/code&gt; within &lt;code&gt;AST Explorer&lt;/code&gt; toggle the &lt;code&gt;transform&lt;/code&gt; option within the top navigation bar. By turning this on you can select different types of transformations, as I am working with &lt;code&gt;jscodeshift&lt;/code&gt; I use that option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;Let’s walk through a simple transformation that will transform &lt;a href="https://canjs.com" rel="noopener noreferrer"&gt;CanJS&lt;/a&gt; &lt;code&gt;Component&lt;/code&gt; into a &lt;code&gt;StacheElement&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component.extend({
 tag: 'my-app',
 view: `&amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;`
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Will become&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyApp extends StacheElement {
 static get view () {
   return `&amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;`
 }
}
customElements.define('my-app', MyApp)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here is a gist with the code used to do the transformation in its most basic form:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let’s try to break this down a little, passing &lt;code&gt;jscodeshift&lt;/code&gt; the source file will generate the &lt;code&gt;AST&lt;/code&gt; and we then call the &lt;code&gt;.find&lt;/code&gt; method which allows us to traverse and find something specific within the tree. It will return a &lt;code&gt;Collection&lt;/code&gt; which you can iterate through and modify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.find(j.CallExpression, {
 callee: {
   type: 'MemberExpression',
   object: {
     name: 'Component'
   },
   property: {
     name: 'extend'
   }
 }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we are looking for a &lt;code&gt;CallExpression&lt;/code&gt; which has a specific &lt;code&gt;callee&lt;/code&gt; and we can specify the type and names of the &lt;code&gt;callee&lt;/code&gt;, in this instance, we are looking for &lt;code&gt;Component.extend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we have the results we can iterate over them and modify to our heart’s content 😊.&lt;/p&gt;

&lt;p&gt;We know the type of result we are going to get and the shape of it, so we can iterate over the properties of the object enabling us to add these back after we have replaced the &lt;code&gt;CallExpression&lt;/code&gt; with a &lt;code&gt;Class&lt;/code&gt; declaration.&lt;/p&gt;

&lt;p&gt;We want to restore the &lt;code&gt;view&lt;/code&gt; property and will add this to the class as a static getter property. Here we keep a reference to the &lt;code&gt;view&lt;/code&gt;’s path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path.value.arguments[0].properties
.forEach(p =&amp;gt; {
   if (p.key.name === 'view') {
     viewProp = p
   }
 })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can replace the &lt;code&gt;CallExpression&lt;/code&gt; with a &lt;code&gt;Class&lt;/code&gt; declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;j(path).replaceWith(
 j.classDeclaration(
   j.identifier(className),
   j.classBody([
     j.methodDefinition(
       'get',
       j.identifier('view'),
       j.functionExpression(
         null,
         [],
         j.blockStatement([j.returnStatement(viewProp.value)])
       ),
       true
     )
   ]),
   j.identifier('StacheElement')
 )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first &lt;code&gt;identifier&lt;/code&gt; is the name of the class, the second &lt;code&gt;identifier&lt;/code&gt; is optional and it’s for adding the name of the &lt;code&gt;superClass&lt;/code&gt;. For the &lt;code&gt;classBody&lt;/code&gt; we will just add what was previously the value of the &lt;code&gt;view&lt;/code&gt; property on the &lt;code&gt;CallExpression&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Full working example can be seen &lt;a href="https://astexplorer.net/#/gist/ac4678db738fbe090bf90f5c4e143d6f/2febfc769e5a3feea737e0436a40595779e2db05" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;p&gt;When you wish to create a Node, use the &lt;code&gt;camelCase&lt;/code&gt; name, and when you wish to look up a Node use the &lt;code&gt;PascalCase&lt;/code&gt; name. For example, if you wish to find a &lt;code&gt;classDeclaration&lt;/code&gt; you would do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;j(file.source)
.find(j.ClassDeclaration)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you wanted to create a class you would do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const classDeclaration = j.classDeclaration(...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Voilà we have a simple transform!!&lt;/p&gt;

&lt;p&gt;Thanks for following along and thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Gatsby Firebase Deployment</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Wed, 05 Jun 2019 09:00:00 +0000</pubDate>
      <link>https://forem.com/mattchewone/gatsby-firebase-deployment-3idi</link>
      <guid>https://forem.com/mattchewone/gatsby-firebase-deployment-3idi</guid>
      <description>&lt;p&gt;Having recently published my personal site with Gatsby I wanted to set up a basic pipeline with Gitlab to deploy my site when I push changes to the &lt;code&gt;master&lt;/code&gt; branch. This will be beneficial so that if I create another branch, the deploy process won’t initiate on other branches.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gitlab CI
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above &lt;code&gt;gist&lt;/code&gt; shows how simple it is to create a basic CI pipeline. Let’s break it down a little though. I only have a deploy task, which uses the latest node container image, then using the script section of the job run &lt;code&gt;npm i&lt;/code&gt; to install all the project dependencies. Then we can install some globals, &lt;code&gt;gatsby-cli&lt;/code&gt; and &lt;code&gt;firebase-tools&lt;/code&gt;, these are used to build the static site and for deployment. To create the site files I would run the &lt;code&gt;gatsby build&lt;/code&gt;, and set up &lt;code&gt;firebase&lt;/code&gt; to use token authentication. We will come back to this in a minute, and then we run the &lt;code&gt;firebase deploy&lt;/code&gt; to… well… deploy the site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Firebase Token Auth
&lt;/h2&gt;

&lt;p&gt;As we are deploying using CI, we need the deployment to be “hands off” and will need to have an authenticated token for the CI to use to access the project and have permission to deploy. Thankfully firebase has a handy way to generate a token for this exact purpose. If you run the following in your terminal, it will open a browser tab and request you grant access to create this token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firebase login:ci
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Firebase will then provide you with a handy token within your terminal, which can be used within &lt;a href="https://gitlab.com/help/ci/variables/README"&gt;Gitlab’s CI variables&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use this token we need to add it to Gitlab’s Variables for CI/CD, which can be accessed via Settings &amp;gt; CI / CD:&lt;/p&gt;

&lt;p&gt;&lt;a href="///static/62f22f4db94c406ec8c0200accb920e3/5dc2c/gitlab-screengrab.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qR1GXZft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mattchaffe.uk/static/62f22f4db94c406ec8c0200accb920e3/f570d/gitlab-screengrab.png" alt="Gitlab CI/CD Variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that I have two variables, &lt;code&gt;FIREBASE_TOKEN&lt;/code&gt; and &lt;code&gt;PROJECT_ID&lt;/code&gt;, these will be injected by Gitlab into the pipeline.&lt;/p&gt;

&lt;p&gt;Once you have added the secrets and created the &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; file, push to &lt;code&gt;master&lt;/code&gt; and this should kick off a build and deploy to firebase! Woop!&lt;/p&gt;

&lt;p&gt;&lt;a href="///static/0b6c03ad95152cfd498ddae5cb1c2b9b/de157/gitlab-pipeline.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PBYI3BoQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mattchaffe.uk/static/0b6c03ad95152cfd498ddae5cb1c2b9b/f570d/gitlab-pipeline.png" alt="Gitlab pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create content…
&lt;/h2&gt;

&lt;p&gt;Now you can create your content, commit, push to gitlab and let it deploy whilst you can sit back and enjoy your new article/blog!&lt;/p&gt;

&lt;p&gt;Awesome!!&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>firebase</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>FeathersJS Emitting</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Mon, 25 Feb 2019 15:20:14 +0000</pubDate>
      <link>https://forem.com/mattchewone/feathersjs-emitting-4mhh</link>
      <guid>https://forem.com/mattchewone/feathersjs-emitting-4mhh</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm10msctqox1hi05t67b5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm10msctqox1hi05t67b5.jpeg" width="800" height="559"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/photos/faUzKz3TvpM?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;rawpixel&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/publish?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FeathersJS makes it really easy to write realtime web API’s. I want to talk about how you can emit data from your hooks. I had an application which had two services groups and group-access , a user could only fetch items from groups if they had a record in the group-access collection.&lt;/p&gt;

&lt;p&gt;But what this meant was when a user was granted access via the group-access service they would have to reload the page in order to re-fetch groups to reflect the new access.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above snippet is for an &lt;a href="https://docs.feathersjs.com/api/hooks.html#registering-hooks" rel="noopener noreferrer"&gt;after create hook&lt;/a&gt;, this would be for the group-access service. When a new group-access record is created this hook will run and will load the group via a get, so we can emit this record. We then emit on the groups service and use the created event name. We clone the context and add some additional params so that we can use these within the groups publish function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be sure to update the path so that it will emit using that path / service name.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above publish snippet, we are only going to publish realtime data if we have come from an internal emit, we can verify this by checking the property we added to the context in the emit call. In a real app you would probably be emitting based on some other checks.&lt;/p&gt;

&lt;p&gt;What this will do is filter all the channels to only that of context.userId which was passed in by the custom emit. It will send that data to the channel.&lt;/p&gt;

&lt;p&gt;If you are using something like can-connect-feathers or feathers-vuex when a realtime created event happens the created item will be added to the list of groups and should be displayed automagically.&lt;/p&gt;

&lt;p&gt;This should be done the same for the removed event, so when a groups-access record is deleted, as such revoking the user access. We can emit a removed event, which if configured the client will be listening for, thus removing the record from the list.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;If you noticed anything incorrect or that could be improved please comment below. I appreciate all constructive feedback.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://monzo.me/matthewjohnchaffe/2.50" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw30gu7kx1kk1zg0gfjsj.gif" width="673" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>feathersjs</category>
      <category>node</category>
      <category>socketio</category>
      <category>javascript</category>
    </item>
    <item>
      <title>FeathersJS Channel Subscriptions</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Tue, 12 Feb 2019 11:38:41 +0000</pubDate>
      <link>https://forem.com/mattchewone/feathersjs-channel-subscriptions-4ma3</link>
      <guid>https://forem.com/mattchewone/feathersjs-channel-subscriptions-4ma3</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobtr5zmc4ka55d9424ek.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobtr5zmc4ka55d9424ek.jpeg" width="800" height="534"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/photos/qMMzHPF3ajQ?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;chuttersnap&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/channels?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://feathersjs.com/" rel="noopener noreferrer"&gt;FeathersJS&lt;/a&gt; Channels are a great way to handle real-time data. However, sometimes you might only want certain data on a certain page. For example, you may have a chatroom app which has multiple rooms and you only want to provide data for a room that a user is viewing.&lt;/p&gt;

&lt;h3&gt;
  
  
  On the server
&lt;/h3&gt;

&lt;p&gt;To be able to handle channels on the fly, I am going to configure a custom service which will handle joining and leaving channels.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above snippet custom service class we use the create method to join the channel and the remove method to leave the channel.&lt;/p&gt;

&lt;h4&gt;
  
  
  Service
&lt;/h4&gt;

&lt;p&gt;Now to configure the service to emit data to the channels, within the *-service.js file, we can configure a custom publish. This will emit data only to the channels which exist and will filter out the current user. So the user that created the message within the room, won’t get an .on('created')event when they create a new message, but all other subscribed users will get this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Awesome! Now we are only emitting data to those that want it. Now let’s take a look at setting up the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  The client
&lt;/h3&gt;

&lt;p&gt;For our discussion here, I am just going to show a brief example of where you would emit the subscribe/unsubscribe events.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above example is of a CanJS component. We use connectedCallback which is a lifecycle hook called after the component’s element is inserted into the document. This can also return a function which will be used when the component is torn down / removed from the document.&lt;/p&gt;

&lt;p&gt;Within the connectedCallback we can emit the subscribe event, and in the teardown, we can emit the unsubscribe. When a user navigates to this component it will automatically subscribe the current user to the room, so all messages that are created with this roomId will be sent to this room channel.&lt;/p&gt;

&lt;p&gt;To see how this affects your app, if you open up the network tab in devtools, and select the ws sub-tab you will see in the frames section all the data transferred to and from your server.&lt;/p&gt;

&lt;p&gt;If the server was configured to publish to the authenticated channel which would be all logged in users. Every user would see created events for messages and rooms which they are not viewing. This means that the server is sending data to those that aren’t using it or require it, so we can prevent this by only sending data to those that are subscribed to specific channel.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;If you noticed anything incorrect or that could be improved please comment below. I appreciate all constructive feedback.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://monzo.me/matthewjohnchaffe/2.50" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw30gu7kx1kk1zg0gfjsj.gif" width="673" height="203"&gt;&lt;/a&gt;&lt;/p&gt;




</description>
      <category>feathersjs</category>
      <category>socketio</category>
      <category>javascript</category>
      <category>realtime</category>
    </item>
    <item>
      <title>React Native OAuth</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Fri, 11 Jan 2019 16:35:21 +0000</pubDate>
      <link>https://forem.com/mattchewone/react-native-oauth-27ph</link>
      <guid>https://forem.com/mattchewone/react-native-oauth-27ph</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zzpVnUOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AdY8bMNoFO3dYrkXUXjfnRw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zzpVnUOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AdY8bMNoFO3dYrkXUXjfnRw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I just want to start with I have little to no experience with React or React Native so there are likely things I am doing which could be improved, but I wanted to learn and have to start somewhere.&lt;/p&gt;

&lt;p&gt;I decided to begin with getting an OAuth workflow within React Native. I’m going to run through this from a Mac OSX user perspective, and for a target of iOS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;Where better to get started than following the Getting Started guide, I initially followed the Expo flow as this was the one which is shown by default. But I soon learned that with OAuth you need to install some native dependencies for the Auth flow.&lt;/p&gt;

&lt;p&gt;To create the app initially it’s fairly straight forward, install the cli npm install -g react-native-cli and then create your project react-native init MyAwesomeAuthApp.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Auth
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Installation
&lt;/h4&gt;

&lt;p&gt;For the Auth, I am going to use the &lt;a href="https://github.com/FormidableLabs/react-native-app-auth"&gt;React Native App Auth&lt;/a&gt; package, which allows OAuth 2.0 and OpenID Connect providers.&lt;/p&gt;

&lt;p&gt;Install and link the native package via the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-native-app-auth
react-native link react-native-app-auth
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The link part is what adds the native dependencies to the ios and andriod builds. I opted to use the Carthage step to add the dependencies as I had some issues with the Cocoa pods not working correctly to install the dependency. Start by navigate into the iOS folder and create a Cartfile with the following contents:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;github "openid/AppAuth-iOS" "master"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You will need to have carthage installed which you can do with brew install carthage. Once you have installed and created the Cartfile you can run carthage update — platform iOS to install the packages.&lt;/p&gt;

&lt;p&gt;Once this has completed you will need to copy the AppAuth.framework which is located in ios/Carthage/Build/iOS and add that to Frameworks within Xcode. Finally setup a Copy Files phase within the Build Phases, pick Frameworks as the destination and add AppAuth.framework and be sure to check the Code Sign on Copy option.&lt;/p&gt;
&lt;h4&gt;
  
  
  Xcode setup
&lt;/h4&gt;

&lt;p&gt;We are going to need to open the ios project in Xcode now to finish the setup. From within the ios folder if you run open MyAuthApp.xcodeproj this should open Xcode.&lt;/p&gt;

&lt;p&gt;Once the project is open, expand the folder which says the project name in my case MyAuthApp you should see a file called AppDelegate.h you can replace it’s contents with the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This sets up the AuthorisationFlowManager, and you will need to update the AppDelegate.m also to allow the openUrl method to feed the authorisation into the app. Before the call to the @end of the AppDelegate.m file add this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This completes the Auth setup and installation, you should now be able to run react-native run-ios and see a running application within a simulator. This may take a while and will confirm that the setup is complete if all goes well. We haven’t added any auth to the actual React app yet, so this is just to clarify that everything worked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Auth
&lt;/h3&gt;

&lt;h4&gt;
  
  
  OAuth flow
&lt;/h4&gt;

&lt;p&gt;We have installed the package and set it all up, now we need to add Auth to the React app. We are going to keep this simple for now and just going to add a button which will run the authentication.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above snippet, I’ve updated the generated App.js file which react-native creates by default. I’ve removed some of the initial text and added a login button. I’ve imported the auth package and created an _authorize method which will open the chosen auth providers login page.&lt;/p&gt;

&lt;p&gt;For the example above I have chosen Google. You will need to set up the &lt;a href="https://developers.google.com/identity/protocols/OAuth2"&gt;OAuth&lt;/a&gt; credentials and add them to the clientId and redirectUrl once you have done this you should be able to reload the app and press the login button to be taken to the Google login screen! Yeah!!&lt;/p&gt;

&lt;p&gt;Thanks for reading. I’m no React expert so if you have anything I’ve done wrong or could be improved I’d appreciate any comments or messages!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Mattchewone/react-native-oauth"&gt;Mattchewone/react-native-oauth&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://monzo.me/matthewjohnchaffe"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M49gyB_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/673/1%2AnkyFXd8M6S3KXTNtFsCylQ.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>auth</category>
      <category>reactnative</category>
      <category>react</category>
    </item>
    <item>
      <title>CanJS &amp; FeathersJS Channels</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Tue, 18 Dec 2018 14:59:44 +0000</pubDate>
      <link>https://forem.com/mattchewone/canjs--feathersjs-channels-4gg5</link>
      <guid>https://forem.com/mattchewone/canjs--feathersjs-channels-4gg5</guid>
      <description>&lt;p&gt;I recently wrote an article about how to useFeathersJS’s channels to ensure the right realtime data goes to the correct user(s). I want to show how to do the same realtime fun but using &lt;a href="https://canjs.com/" rel="noopener noreferrer"&gt;CanJS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I’ll refer to this article on how to setup the FeatherJS channels:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.feathersjs.com/feathersjs-channels-vuex-7548fb5c5d2c" rel="noopener noreferrer"&gt;FeathersJS, Channels &amp;amp; Vuex&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting setup with CanJS
&lt;/h3&gt;

&lt;p&gt;I cloned this &lt;a href="https://github.com/canjs/stealjs-example" rel="noopener noreferrer"&gt;repo&lt;/a&gt; to get started.&lt;/p&gt;

&lt;p&gt;Let’s start at setting up the models so we can load the data and get the realtime goodness. We will first need to create a feathersConnection which is a set of custom can-connect behaviours.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above will be used for models to fetch data and keep the model up to date with realtime data. This will also handle combining multiple requests into a single request and a few other cool things.&lt;/p&gt;

&lt;p&gt;There is a similar one needed for authentication&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This will handle logging in and getting a user object once logged in.&lt;/p&gt;

&lt;h4&gt;
  
  
  Models
&lt;/h4&gt;

&lt;p&gt;The user model we can setup using the above feathers-connection like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We define the properties on L6-L13 and on L15 we create a reactive list with each list item being an instance of User. The list itself has some computed properties, so we can get usersById and usersByEmail.&lt;/p&gt;

&lt;p&gt;On L34-L41 we setup the connection details for this model which tells it how to get data. We pass it the feathers-service we want it to use to fetch data.&lt;/p&gt;

&lt;p&gt;The session / authentication model is similar but it uses feathers-authentication to create the connection:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We create a userPromise async getter, which will load the user if the userId exists, this will allow us within the user prop to load in a user, which will be an instance of the User model we defined earlier.&lt;/p&gt;

&lt;p&gt;Finally we create a message model which will handle loading in message data.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We are using &lt;a href="https://canjs.com/doc/can-query-logic.html" rel="noopener noreferrer"&gt;can-query-logic&lt;/a&gt; along with feathers-query-logic to handle converting feathers queries into a query format that can-connect can use to query data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting the data
&lt;/h3&gt;

&lt;p&gt;So far we have discussed getting the models setup so we can load in data, let’s see how that’s done within a component.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above is the ViewModel for the MessageList component. We create a usersPromise and a messagesPromise which will load in the initial messages and users for the page load. We need the users so we can map the email within the message to the users name.&lt;/p&gt;

&lt;p&gt;We create a getter which will Promise.all both queries so we can load them both before rendering the list of messages. Using the connectedCallback lifecycle method of the ViewModel we create a listenTo event listener, which will fire once a property changes. Once the current user is present on the ViewModel we can then load the initial data.&lt;/p&gt;

&lt;p&gt;Now that we have the initial data loaded, we can render this within the template. When we create new data or retrieve new data via sockets the Message model’s list will automatically be updated, and the data will update within the template!&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating new messages
&lt;/h4&gt;

&lt;p&gt;We can call new Message({ ...data }) to create a new instance, and calling .save() will send this to the server and update our Message.List. As this is a promise, we can .then to reset the input bindings so the form is clear for another message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new Message({ to: this.to, message: this.msg })
.save()
.then(() =\&amp;gt; {
 this.to = ''
 this.msg = ''
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AA_IN6v1o_5HNlBILUadKrQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AA_IN6v1o_5HNlBILUadKrQ.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see the full repo here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Mattchewone/realtime-canjs" rel="noopener noreferrer"&gt;Mattchewone/realtime-canjs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please comment or ask questions!&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>socketio</category>
      <category>javascript</category>
      <category>realtime</category>
      <category>canjs</category>
    </item>
    <item>
      <title>Feathersjs background hooks</title>
      <dc:creator>Matt Chaffe</dc:creator>
      <pubDate>Fri, 09 Mar 2018 18:17:15 +0000</pubDate>
      <link>https://forem.com/mattchewone/feathersjs-background-hooks--1j76</link>
      <guid>https://forem.com/mattchewone/feathersjs-background-hooks--1j76</guid>
      <description>&lt;h5&gt;
  
  
  Feathersjs is an open source REST and realtime API layer for modern applications.
&lt;/h5&gt;

&lt;p&gt;If you require hook(s) to run after you have responded to the client, for example with a process that is likely to take a few seconds to complete and the response of the hooks isn’t required by the actual service being called, then you can return the &lt;code&gt;context&lt;/code&gt; from the hook and allow the function to be run.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Running a single hook is relatively straightforward as the above hook shows. But I required to run multiple hooks in sequence, with each one depending on the previous one.&lt;br&gt;
Here is where &lt;code&gt;combine from the&lt;/code&gt;feathers-hooks-common` comes in handy&lt;/p&gt;

&lt;p&gt;{% gist &lt;a href="https://gist.github.com/Mattchewone/0931d8903a4bcdde675851a2e5a6e173"&gt;https://gist.github.com/Mattchewone/0931d8903a4bcdde675851a2e5a6e173&lt;/a&gt; %}&lt;/p&gt;

&lt;p&gt;The above hook shows a rather simplistic hook that will look up all records with a name property, it will add them to the params object.&lt;/p&gt;

&lt;p&gt;{% gist &lt;a href="https://gist.github.com/Mattchewone/4215286d23bf90fc8131f768253de229"&gt;https://gist.github.com/Mattchewone/4215286d23bf90fc8131f768253de229&lt;/a&gt; %}&lt;/p&gt;

&lt;p&gt;This second hook will filter out existing names so we are left with new names to create. Say we had a large amount of names to create or we had some hooks that ran on the names being created and we didn’t wish to wait for the create to finish before we responded to the client from the initial request.&lt;/p&gt;

&lt;p&gt;{% gist &lt;a href="https://gist.github.com/Mattchewone/297caaea9eb40c9d0a03318d3dcd88b0"&gt;https://gist.github.com/Mattchewone/297caaea9eb40c9d0a03318d3dcd88b0&lt;/a&gt; %}&lt;/p&gt;

&lt;p&gt;We can &lt;code&gt;combine&lt;/code&gt; the hooks and return the &lt;code&gt;context&lt;/code&gt; immediately, the hooks will run “in the background”. The response will be sent to the client whilst the hooks are still being processed by the event loop.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>feathersjs</category>
    </item>
  </channel>
</rss>
