<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>colinramsay.co.uk</title>
		<description>My personal site</description>		
		<link>http://colinramsay.co.uk</link>
		<atom:link href="http://colinramsay.co.uk/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Capturing my handwriting in a searchable digital format - the long way round</title>
				<description>&lt;h2 id=&quot;why-handwriting&quot;&gt;Why handwriting?&lt;/h2&gt;

&lt;p&gt;I’ve been spending a lot of time in a classroom environment and taking notes. My preferred way to do this is in a notepad, as despite leading a pretty digitised life I find taking notes like this to be the lowest friction solution. Plus, who wants to listen to someone tip-tapping away on a keyboard, right?&lt;/p&gt;

&lt;h2 id=&quot;why-digitise-it&quot;&gt;Why digitise it?&lt;/h2&gt;

&lt;p&gt;I found that a lot of my notes were either being forgotten or it was hard to find where I’d written a particular thing. By having my notes searchable, I would be able to look up key words and have indexes, etc.&lt;/p&gt;

&lt;h2 id=&quot;why-create-something-new&quot;&gt;Why create something new?&lt;/h2&gt;

&lt;p&gt;Here’s the big question. When I started thinking about this I assumed there would be something off the shelf - and there is. It just turns out that the options just aren’t very good.&lt;/p&gt;

&lt;h3 id=&quot;apple&quot;&gt;Apple&lt;/h3&gt;

&lt;p&gt;iOS has a thing called &lt;a href=&quot;https://support.apple.com/en-gb/HT212630&quot;&gt;Live Text&lt;/a&gt; which lets you extract text from images, and this was my first port of call. For handwriting, the results are extremely poor and aren’t formatted well, so I moved on.&lt;/p&gt;

&lt;p&gt;I also looked at the handwriting recognition feature of an Apple Pencil but this is too far into the uncanny valley of writing for me, and the results were really mixed.&lt;/p&gt;

&lt;h3 id=&quot;rocketbook&quot;&gt;Rocketbook&lt;/h3&gt;

&lt;p&gt;I picked one of these up - it’s like a whiteboard but in notepad form - and tried out its app which comes with an optical character recognition (OCR) feature. This works really well, but where it fell down was in its sharing options. I wanted to push my notes into &lt;a href=&quot;https://obsidian.md/&quot;&gt;Obsidian&lt;/a&gt;, which doesn’t have an API, and Rocketbook only supports sending directly to email, iCloud drive, Google Drive, and a couple of notes apps like OneNote. So, a couple of problems there.&lt;/p&gt;

&lt;p&gt;Let’s break this down.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I want good quality OCR&lt;/li&gt;
  &lt;li&gt;I want to import my notes into Obsidian&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-first-solution-and-yes-it-involves-ai&quot;&gt;The (first) solution, and yes, it involves AI&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://claude.ai/&quot;&gt;Claude&lt;/a&gt; will convert text in any image into Markdown with really great accuracy, so I knew I wanted to involve that. For Obsidian, given that it doesn’t have an API since it’s local-first, I needed to make a plugin to pull down that Markdown from somewhere and insert it into my Obsidian vault.&lt;/p&gt;

&lt;p&gt;So, I created a Rust webservice that you can POST an image to, and then that sends it to Claude’s API for OCR. The result is pushed into a database. The Obsidian plugin will check a second endpoint on the API and create a new file with that content. When it pulls the content, the API marks it as downloaded so it doesn’t get pulled down again. The plugin also calls the endpoint on a schedule so it can stay up to date.&lt;/p&gt;

&lt;p&gt;And yes, this was an experiment with &lt;a href=&quot;https://www.anthropic.com/claude-code&quot;&gt;Claude Code&lt;/a&gt;. This is clunky but remarkably effective, with it churning out the Obsidian plugin without requiring much bullying. The Rust API required a little more back-and-forth. I have no plans to publish this for other people to use, but I have published the cod-&lt;/p&gt;

&lt;h2 id=&quot;but-wait&quot;&gt;BUT WAIT&lt;/h2&gt;

&lt;p&gt;After I wrote the draft of this post, I sat myself down and gave myself a strong talking to. This seemed so convoluted, &lt;em&gt;and&lt;/em&gt; it was restricted to Obsidian, when all I really wanted was a better version of Apple’s Live Text. So, I spent 45 minutes doing exactly that with iOS’s Shortcuts app, which uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Get Contents Of URL&lt;/code&gt; to make a request to the Claude API and then the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dictionary&lt;/code&gt; features to parse the response and pass it for sharing to another app. This &lt;em&gt;is better&lt;/em&gt;, and while the Shortcuts app is clunky I never have to touch it again.&lt;/p&gt;

&lt;p&gt;I suppose there are two lessons here: the one I learned when using Claude Code to experiment, and the one I’ll hopefully learn in future (and should have already learned in the past): go for the simple(r) tools.&lt;/p&gt;
</description>
				<pubDate>Mon, 23 Jun 2025 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2025/06/23/handwriting-ocr.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2025/06/23/handwriting-ocr.html</guid>
			</item>
		
			<item>
				<title>Source Controlled Linux</title>
				<description>&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;/h2&gt;

&lt;p&gt;I have a passionate desire to &lt;em&gt;fix&lt;/em&gt; the current state of my Linux installation, and to be able to know when files have changed and either revert the changes or store them in source control. Why do I want to do this? Several reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I like the idea that my system is &lt;em&gt;disposable&lt;/em&gt;. It should be easy to blow away a Linux install because I know I can restore it easily.&lt;/li&gt;
  &lt;li&gt;Rather than being an assemblage of cruft built up over the years, I want it to be surgically pruned as each change is made, with comments where necessary to help me remember why a change was made.&lt;/li&gt;
  &lt;li&gt;I don’t want the installation of a package to do unknown things to my system - I like transparency.&lt;/li&gt;
  &lt;li&gt;I just like the idea of it. Stop messing with my stuff, stuff.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-options&quot;&gt;The options&lt;/h2&gt;

&lt;h3 id=&quot;nixnixos&quot;&gt;Nix/NixOS&lt;/h3&gt;

&lt;p&gt;I’ve posted several times about &lt;a href=&quot;https://nixos.org/&quot;&gt;NixOS&lt;/a&gt; and I’m still fascinated by the idea. My last attempt with it followed the “&lt;a href=&quot;https://grahamc.com/blog/erase-your-darlings/&quot;&gt;Erase Your Darlings&lt;/a&gt;” concept, which uses a temporary filesystem that wipes out any changes you haven’t explicitly configured. As has been written about at length, Nix is a wonderful &lt;em&gt;idea&lt;/em&gt;, with a brilliant technical implementation, but terrible user-friendliness. Inevitably you’ll come across something that takes hours of debugging and fiddling, and ultimately life’s too short.&lt;/p&gt;

&lt;p&gt;I also investigated using Nix with &lt;a href=&quot;https://nix-community.github.io/home-manager/&quot;&gt;Home Manager&lt;/a&gt; on a normal Linux distro. This is a worst-of-both-worlds situation, in which Nix’s shebang doesn’t play nicely with everything else. As a side note, Home Manager should be integrated into Nix itself.&lt;/p&gt;

&lt;h3 id=&quot;fedora-silverblue-universal-blue-and-the-like&quot;&gt;Fedora Silverblue, Universal Blue, and the like&lt;/h3&gt;

&lt;p&gt;Here’s what I understand: &lt;a href=&quot;https://fedoraproject.org/silverblue/&quot;&gt;Silverblue&lt;/a&gt; is an immutable Linux distribution that allows you to update in an atomic fashion, and roll back in the event of problems. It allows the user to install applications with Flatpak, isolating those applications from the system at large. Universal Blue takes this concept and produces several derived images such as Bluefin or Bazite, which offer variations of Silverblue for things like gaming. In addition to that, it purports to allow the production of custom images which include changes that you opt to include.&lt;/p&gt;

&lt;p&gt;I’m going to caveat this section by saying that I’d like to do more investigation into this genre of work. That said, I feel a bit hampered by the documentation around Universal Blue, which I feel is in its infancy, and that does put me off.&lt;/p&gt;

&lt;p&gt;The idea of *Blue is really interesting. By embracing container technologies, there’s scope to have an extremely flexible system, and bringing immutability into the mainstream is a valuable effort. My limited experience wasn’t very positive: I tried to create a customised Bluefin image, which saw me jump through a series of rube-goldberg hoops within GitHub Actions to try and get a new image to rebase to. At the end of this process, the resulting image had issues and I had no idea how to debug this. Yes, I could revert to the previous image, but I was still stumped as to how to progress. Changing my image customisations means waiting for the GitHub Actions to run again, and rebuild my image, which is a 30-minute+ process. That’s a huge iteration loop.&lt;/p&gt;

&lt;p&gt;The infrastructure that has been built for this project is incredible, but even with comprehensive documentation I’m unconvinced that anyone outside of the core group will be able to run with this in a practical fashion.&lt;/p&gt;

&lt;h3 id=&quot;arch-via-endeavouros-with-aconfmgr&quot;&gt;Arch (via EndeavourOS) with aconfmgr&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/CyberShadow/aconfmgr&quot;&gt;This works&lt;/a&gt; by examining your entire system and creating a giant file which describes the files it found. You then need to sort that file as you see fit, and then run it periodically to check what’s changed and sort everything again. I tried this briefly a while back, and the overhead of the sorting process and the fact I had to remember to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aconfmgr save&lt;/code&gt; every so often made it a bit of a non-starter.&lt;/p&gt;

&lt;h3 id=&quot;simple-configuration-file-management&quot;&gt;Simple configuration file management&lt;/h3&gt;

&lt;p&gt;This heading covers a class of utilities that will help you back up key configuration files from your system. This approach is typically used for &lt;a href=&quot;https://wiki.archlinux.org/title/Dotfiles&quot;&gt;dotfiles&lt;/a&gt; using tools such as &lt;a href=&quot;https://github.com/lra/mackup&quot;&gt;Mackup&lt;/a&gt;, &lt;a href=&quot;https://www.chezmoi.io/&quot;&gt;Chezmoi&lt;/a&gt; or just plain Git.
&lt;a href=&quot;https://wiki.archlinux.org/title/etckeeper&quot;&gt;EtcKeeper&lt;/a&gt; is a tool which does a similar thing but for the contents of your /etc directory; I’m currently evaluating both Mackup and EtcKeeper as a simple solution to all of this.&lt;/p&gt;

&lt;h2 id=&quot;sidebar-1---declare-vs-discover&quot;&gt;Sidebar 1 - Declare vs discover&lt;/h2&gt;

&lt;p&gt;To my mind, there are two ways of looking at this problem:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Declare what you want, execute a command, and have it implemented (Nix/Silverblue)&lt;/li&gt;
  &lt;li&gt;Run some stuff, then later, run something else that checks what you did and scripts it out (the others)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Looking at it this way, I suppose I should also be looking at things like Ansible or Puppet for the first option. I’ve briefly used Ansible and it was fine for &lt;em&gt;initial setup&lt;/em&gt;. Where I think it might be lacking is for maintenance, since when you make changes to the system there’s scope for that to drift from what Ansible/Puppet thinks is the current state. They also both &lt;em&gt;seem&lt;/em&gt; to be geared towards server/fleet management. I have limited experience here, so this can go on the todo list.&lt;/p&gt;

&lt;h2 id=&quot;sidebar-2---secrets-management&quot;&gt;Sidebar 2 - Secrets management&lt;/h2&gt;

&lt;p&gt;I’ve deliberately skipped over secrets management as that’s a whole can of worms I’ve yet to really think about.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;There’s nothing here that satisfies my needs. NixOS and UBlue are impressive technical achievements, but are currently impenetrable for “normal” Linux users. The other options operate on the “discover” rather than “describe” methodology and appeal to me a lot less.
&lt;!--stackedit_data:
eyJoaXN0b3J5IjpbMjA1NDMwOTQwMyw3ODEzNTEzNTAsMTc0OT
I2ODMzXX0=
--&gt;&lt;/p&gt;
</description>
				<pubDate>Wed, 03 Jan 2024 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2024/01/03/source-controlled-linux.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2024/01/03/source-controlled-linux.html</guid>
			</item>
		
			<item>
				<title>My 2023 Setup</title>
				<description>&lt;p&gt;I always enjoy looking at other how other people have their workstations set up, from their &lt;a href=&quot;https://reddit.com/r/battlestations/&quot;&gt;battlestation&lt;/a&gt; to their hacked-together &lt;a href=&quot;https://www.reddit.com/r/unixporn/&quot;&gt;Linux setup&lt;/a&gt;, and more detailed looks at what &lt;a href=&quot;https://uses.tech/&quot;&gt;people&lt;/a&gt; &lt;a href=&quot;https://uses.tech/&quot;&gt;use&lt;/a&gt;. I’ve been pretty much settled on the same setup for a while now, and this post breaks it down.&lt;/p&gt;

&lt;h2 id=&quot;hardware&quot;&gt;Hardware&lt;/h2&gt;

&lt;p&gt;I’ve got an AMD processor and some graphics card. I bought it off-the shelf and to be honest my days of hand-selecting individual components for the PC itself is mostly done. I did select an M2 drive for added speed, and later added a large SATA drive for more storage.&lt;/p&gt;

&lt;p&gt;That’s the least interesting part of the hardware for me. The biggest part is the monitor setup - one Dell monitor in landscape and another in portrait to the left. This gives me a normal screen for browsing and standard applications, and a second which is purely for viewing code. Long documents work really well on a portrait orientation and I find it perfect for coding.&lt;/p&gt;

&lt;p&gt;I’ve got a &lt;a href=&quot;https://www.keychron.com/products/keychron-k8-pro-qmk-via-wireless-mechanical-keyboard&quot;&gt;Keychron K8 Pro QMK/VIA&lt;/a&gt; which I’d anticipated tweaking with QMK, but that’s not proven necessary. Its slinky RGB backlighting and satisfying mechanical action’s enough for me. My other method of input is a Logitech trackball that’s been a stalwart of my setup since I began experiencing RSI from a mouse. I don’t use any of its additional buttons and aside from occasional clogging, it’s great.&lt;/p&gt;

&lt;p&gt;I’ve recently bought a Sony WH-1000XM5 headset to replace Airpods Pro which had a load of noise when using noise canceling, ironically. This headset is fine, and comfortable, and does have a good feature where it will connect to both phone and desktop simultaneously.&lt;/p&gt;

&lt;p&gt;Other than that, I have a small light above the desk, a deskpad from Ikea which has some cartoon animals on, and a docking station to charge my watch and phone. I’ve also got a Logitech Brio Ultra webcam.&lt;/p&gt;

&lt;h2 id=&quot;os&quot;&gt;OS&lt;/h2&gt;

&lt;p&gt;I currently run &lt;a href=&quot;https://endeavouros.com/&quot;&gt;EndeavorOS&lt;/a&gt; which is based on Arch. I settled on Arch varients after a brief foray into NixOS, since I like the simplicity. When I installed Endeavor, I picked the &lt;a href=&quot;https://i3wm.org/&quot;&gt;i3&lt;/a&gt; install, which is my preferred window manager.&lt;/p&gt;

&lt;p&gt;Why? Because I enjoy having a low-overhead way of creating “workspaces” for various tasks. Workspaces 1-5 are on the left monitor for browsing (workspace 1), messaging (2), terminals (3); workspaces 6-9 are on the right monitor for various code windows. There’s no animation and everything’s lightning-fast and exactly where I put it. I also use the scratchpad for my daily todo list.&lt;/p&gt;

&lt;h2 id=&quot;other-software&quot;&gt;Other software&lt;/h2&gt;

&lt;p&gt;VSCode for coding, with ESLint+Prettier for formatting. The Remote Development extensions from Microsoft come in handy when I’m working on Ruby or C#. DBeaver for database management - the UI is awful but powerful. I’m tracking &lt;a href=&quot;https://tableplus.com/linux&quot;&gt;TablePlus&lt;/a&gt;, which used to be my go-to on Mac but the Linux version is still in development. I use GMail in tabbed windows on workspace 2 for email, since I haven’t found a client I actually like. &lt;a href=&quot;https://www.beeper.com/&quot;&gt;Beeper&lt;/a&gt; is my messaging solution, combining iMessage, Signal Facebook Messenger and Slack into one UI.&lt;/p&gt;

&lt;h2 id=&quot;notes--conclusion&quot;&gt;Notes &amp;amp; Conclusion&lt;/h2&gt;

&lt;p&gt;I do have a Windows 10 partition which I use sometimes (mainly for gaming), but it is inferior to my Linux setup in every way. If there was a way to use NixOS or something similar to have my configuration as code, I’d use that, but all of the options have various weaknesses, so I tend to keep my configuration lightweight and not worry about wiping it.&lt;/p&gt;

&lt;p&gt;There’s not a great deal of new and shiny in this setup. It works, it works fast, and it’s easy to put back together when something fails.&lt;/p&gt;
</description>
				<pubDate>Fri, 25 Aug 2023 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2023/08/25/my-setup.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2023/08/25/my-setup.html</guid>
			</item>
		
			<item>
				<title>Migrating a NixOS Install to Flakes</title>
				<description>&lt;p&gt;I’ve got a &lt;a href=&quot;https://github.com/colinramsay/nix-config/&quot;&gt;working NixOS setup&lt;/a&gt; which I’d like to convert to use Flakes in order to improve reproduceablility. I had a scenario recently whereby I updated the channels I was subscribed to and then rebuilt, and had a build error. There’s no way to roll back the channel update in that case. Flakes should fix this, because my current build will pin the commit hashes of versions it was built with.&lt;/p&gt;

&lt;p&gt;The information in this post can be found in various wikis, gists and blog posts. I’m just going to explain how I got it to work on my setup with the aid of some diffs.&lt;/p&gt;

&lt;p&gt;Since Flakes are still experimental, we need to add them to our configuration:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;--- a/configuration.nix
+++ b/configuration.nix
@@ -5,6 +5,14 @@
 { config, pkgs, ... }:
 
 {
+  nix = {
+    package = pkgs.nixUnstable;
+    extraOptions = &apos;&apos;
+      experimental-features = nix-command flakes
+    &apos;&apos;;
+   };
+

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

&lt;p&gt;And then rebuild and switch to this new configuration with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo nixos-rebuild switch&lt;/code&gt;. I can then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nix flake init&lt;/code&gt; to get a new flake.nix file, but we’re going to ignore the contents of that and use the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  # the source of your packages
  inputs = {
    # normal nix stuff
    nixpkgs.url = &quot;github:NixOS/nixpkgs/nixos-21.05&quot;;
    # home-manager stuff
    home-manager.url = &quot;github:nix-community/home-manager/release-21.05&quot;;

    # use the version of nixpkgs we specified above rather than the one HM would ordinarily use
    home-manager.inputs.nixpkgs.follows = &quot;nixpkgs&quot;;
  };

  # what will be produced (i.e. the build)
  outputs = inputs: {
    # define a &quot;nixos&quot; build
    nixosConfigurations.nixos = inputs.nixpkgs.lib.nixosSystem {
      # system to build for
      system = &quot;x86_64-linux&quot;;
      # modules to use
      modules = [
        ./configuration.nix # our previous config file
        inputs.home-manager.nixosModules.home-manager # make home manager available to configuration.nix
        {
          # use system-level nixpkgs rather than the HM private ones
          # &quot;This saves an extra Nixpkgs evaluation, adds consistency, and removes the dependency on NIX_PATH, which is otherwise used for importing Nixpkgs.&quot;
          home-manager.useGlobalPkgs = true;
        }
      ];
    };
  };
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then remove the existing reference to home-manager from configuration.nix which Flakes considers to be impure:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;--- a/configuration.nix
+++ b/configuration.nix
@@ -117,7 +117,7 @@
 
   imports = [ # Include the results of the hardware scan.
     ./hardware-configuration.nix
-    &amp;lt;home-manager/nixos&amp;gt;
+    #&amp;lt;home-manager/nixos&amp;gt;
   ];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;https://github.com/colinramsay/nix-config/commit/116f06c2bf30e9ce65f27323c77d2bee351aa755&lt;/p&gt;

&lt;p&gt;Now if you rebuild, nix will detect the flake.nix file and start downloading a bunch of stuff and build your system, depositing a flake.lock file in your working directory which you can commit to source control. This contains the revisions of nixpkgs and home-manager that the system was built from so it can be rebuilt if necessary.&lt;/p&gt;
</description>
				<pubDate>Tue, 19 Oct 2021 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2021/10/19/migrating-a-nixos-install-to-flakes.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2021/10/19/migrating-a-nixos-install-to-flakes.html</guid>
			</item>
		
			<item>
				<title>Many-To-Many Tags or Categories With Elixir &amp; Phoenix</title>
				<description>&lt;p&gt;I want to allow “questions” to have multiple “categories”. We need a join table for that, so let’s generate a migration:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mix ecto.gen.migration add_questions_to_categories
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then in our new migration:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;defmodule Quizzical.Repo.Migrations.AddQuestionsToCategories do
  use Ecto.Migration

  def change do
    create table(:questions_categories, primary_key: false) do
      add :question_id, references(:questions, on_delete: :delete_all)
      add :category_id, references(:categories, on_delete: :delete_all)
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We’re using &lt;a href=&quot;https://hexdocs.pm/ecto_sql/Ecto.Migration.html#references/2&quot;&gt;references&lt;/a&gt; to create foreign keys and enforce deletion of join records at the database level. Then, in our pre-existing “question” schema we use the many_to_many macro:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;many_to_many :categories, Category, join_through: &quot;questions_categories&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And likewise in our “category” schema:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;many_to_many :questions, Question, join_through: &quot;questions_categories&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The documentation on &lt;a href=&quot;https://hexdocs.pm/ecto/Ecto.Schema.html#many_to_many/3&quot;&gt;many_to_many&lt;/a&gt; goes into more detail but we’re only specifying the name of our join table and the schema representing the “other side” of the join.&lt;/p&gt;

&lt;p&gt;When we create or update a question, the parameters coming in from the UI are going to look like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;%{
    &quot;answer&quot; =&amp;gt; &quot;Because&quot;,
    &quot;categories&quot; =&amp;gt; [&quot;1&quot;, &quot;Film&quot;, &quot;Art&quot;],
    &quot;question&quot; =&amp;gt; &quot;Why&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We’re going to make the assumption that if one of the items in the “categories” array is an integer then that’s a category ID. If it’s a string, then it’s a new category we need to create and we’ll use the string as its name. Let’s try that using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iex&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ iex -S mix
$ %Question{} |&amp;gt; Question.changeset(%{ question: &quot;Why&quot;, answer: &quot;Cos&quot;, categories: [&quot;that&quot;, &quot;this&quot;] })
#Ecto.Changeset&amp;lt;
  action: nil,
  changes: %{answer: &quot;Cos&quot;, question: &quot;Why&quot;},
  errors: [],
  data: #Quizzical.Questions.Question&amp;lt;&amp;gt;,
  valid?: true
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While I’d already set up the changeset to accept the question and answer fields, it now needs to accept categories too. We’ll use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;put_assoc/3&lt;/code&gt; for that, passing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;categories&lt;/code&gt; from the UI params to a function called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert_and_get_all_categories/1&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def changeset(question, attrs) do
  categories = attrs[&quot;categories&quot;] || []

  question
  |&amp;gt; cast(attrs, [:question, :answer])
  |&amp;gt; validate_required([:question, :answer])
  |&amp;gt; Ecto.Changeset.put_assoc(:categories, insert_and_get_all_categories(categories))
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert_and_get_all_categories/1&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; defp insert_and_get_all_categories(names) do
    # Convert the array of category names into an array of { name: &quot;name&quot; } maps
    maps = Enum.map(names, &amp;amp;%{name: &amp;amp;1})
    
    # Insert all of those categories into the database, if theres&apos;s a naming conflict
    # just ignore it. That means we can insert all the categories each time and not care about duplicates.
    Repo.insert_all(Category, maps, on_conflict: :nothing)
    
    # Now return full categories objects matching the passed-in names, because `put_assoc` expects
    # category objects.
    Repo.all(from c in Category, where: c.name in ^names)
  end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For this to work, you need to have a unique index on category name, so if you don’t, create a new migration:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;defmodule Quizzical.Repo.Migrations.AddUniqueCategoryNameIndex do
  use Ecto.Migration

  def change do
    create unique_index(:categories, [:name])
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And finally, we need to let Ecto know what to do when a category on a question is being “replaced”. “Replaced is described by the &lt;a href=&quot;https://hexdocs.pm/ecto/Ecto.Changeset.html#module-the-on_replace-option&quot;&gt;documentation&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;When using any of those APIs, you may run into situations where Ecto sees data is being replaced. For example, imagine a Post has many Comments where the comments have IDs 1, 2 and 3. If you call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cast_assoc/3&lt;/code&gt; passing only the IDs 1 and 2, Ecto will consider 3 is being “replaced” and it will raise by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great, so when we’re “replacing” a category we’re actually deliberately not passing one from the UI, which means we want to remove it from the question. To do that we just specify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on_replace&lt;/code&gt; option in the question schema:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;many_to_many :categories, Category, join_through: &quot;questions_categories&quot;, on_replace: :delete
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Done! The majority of this comes from José Valim’s &lt;a href=&quot;https://blog.plataformatec.com.br/2016/12/many-to-many-and-upserts/&quot;&gt;post on many-to-many&lt;/a&gt;.&lt;/p&gt;
</description>
				<pubDate>Fri, 12 Feb 2021 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2021/02/12/many-to-many-tags-ecto-phoenix.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2021/02/12/many-to-many-tags-ecto-phoenix.html</guid>
			</item>
		
			<item>
				<title>Auth0 Metadata In Userinfo Api</title>
				<description>&lt;p&gt;Auth0’s &lt;a href=&quot;https://auth0.com/docs/api/authentication#get-user-info&quot;&gt;Get User Info&lt;/a&gt; endpoint does not include any user or app metadata. In order to do this, you can create a rule in their dashboard:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function (user, context, callback) {
  const namespace = &apos;https://changeme/&apos;;
  
  context.idToken[namespace + &apos;user_metadata&apos;] = user.user_metadata;
  context.idToken[namespace + &apos;app_metadata&apos;] = user.app_metadata;
  
  callback(null, user, context);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The namespace is arbitrary. One very important detail: rules only run when the user authenticates so if you try this you’ll need to log the user out then back in again after saving the rule.&lt;/p&gt;
</description>
				<pubDate>Wed, 08 Jul 2020 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2020/07/08/auth0-metadata-in-userinfo-api.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2020/07/08/auth0-metadata-in-userinfo-api.html</guid>
			</item>
		
			<item>
				<title>Docker For Ruby</title>
				<description>&lt;p&gt;Create a Dockerfile:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FROM ruby:2.6

WORKDIR /app

COPY Gemfile ./
RUN bundle install

COPY . /app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A handy Makefile:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PREFIX = docker run -v &quot;$(shell pwd)&quot;:/app --rm --name appname imagename

build:
	docker build -t imagename .

test: build
	$(PREFIX) bundle exec rspec spec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create a Gemfile and add some gems to it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bundle init
bundle add rspec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And you can now create the following directory structure in your local folder&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lib/my_class.rb
spec/my_class_spec.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Write some specs and run them with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;make test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Huzzah!&lt;/p&gt;
</description>
				<pubDate>Wed, 03 Jul 2019 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2019/07/03/docker-for-ruby.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2019/07/03/docker-for-ruby.html</guid>
			</item>
		
			<item>
				<title>Yodlee Fastlink Postmessage Not Working?</title>
				<description>&lt;p&gt;The documentation for &lt;a href=&quot;https://developer.yodlee.com/Yodlee_API/FastLink/FastLink_3.0_Integration_Guide&quot;&gt;Yodlee’s Fastlink&lt;/a&gt; leaves something to be desired. It mentions that you can use &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage&quot;&gt;window.postMessage&lt;/a&gt; to receive communications from the Fastlink iFrame, but in reality these never arrive. Unless…&lt;/p&gt;

&lt;p&gt;Having rummaged through the Fastlink JavaScript source, I found that the postMessage would not be sent unless you specified a parameter called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;locationurl&lt;/code&gt;, something that’s only supposed to be necessary if you want to resize the iFrame. It also wasn’t clear how to add this new parameter. Turns out you have to jam it in to an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extraParams&lt;/code&gt; parameter:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;input type=&quot;hidden&quot; name=&quot;extraParams&quot; value=&quot;locationURL=&amp;lt;Your calling URL&amp;gt;&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hopefully this saves someone else from wasting several hours while being ignored by Yodlee’s support staff.&lt;/p&gt;

&lt;p&gt;UPDATE! &lt;em&gt;Two weeks&lt;/em&gt; after I requested support, Yodlee sent me this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Apologies for the delayed response. The sandbox environment doesn’t enable the post messages on the FastLink iFrame, which is why you’re not seeing a response.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Seems like they don’t know their own product too well…&lt;/p&gt;
</description>
				<pubDate>Wed, 25 Jul 2018 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2018/07/25/yodlee-fastlink-postmessage.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2018/07/25/yodlee-fastlink-postmessage.html</guid>
			</item>
		
			<item>
				<title>Go Serverless With React Native and Amazon Web Services</title>
				<description>&lt;p&gt;This post is also available on the blog of &lt;a href=&quot;http://www.gotripod.com/go-tripod-go-serverless&quot;&gt;my software company, Go Tripod&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Amazon’s cloud offering, AWS, provides a wealth of resources for developers and infrastructure engineers. In this article, I’ll show how &lt;a href=&quot;aws.amazon.com/cognito/&quot;&gt;Amazon Cognito&lt;/a&gt; can be used to record user preferences and data without having to set up a server or database. I’ll build a small React Native application which allows a user to log in with Facebook and save some data to the cloud for later retrieval.&lt;/p&gt;

&lt;h2 id=&quot;facebook-login&quot;&gt;Facebook Login&lt;/h2&gt;

&lt;p&gt;Prerequisites:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Install npm and CocoaPods if you don’t have them already&lt;/li&gt;
  &lt;li&gt;Install &lt;a href=&quot;https://facebook.github.io/react-native/docs/getting-started.html#content&quot;&gt;React Native&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Create a new Facebook iOS app in the &lt;a href=&quot;https://developers.facebook.com/apps/&quot;&gt;developer centre&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s start by creating our React Native project and entering the project directory:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;react-native init FacebookAwsReactNative
cd FacebookAwsReactNative
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s install the &lt;a href=&quot;https://github.com/facebook/react-native-fbsdk&quot;&gt;Facebook React Native SDK&lt;/a&gt; via npm, that’ll give us a nice login button for free:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm install --save react-native-fbsdkcore react-native-fbsdklogin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We need to install some native code to make this work, so let’s do that with CocoaPods. First we create a Podfile:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ios
pod init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit the file which was just created at FacebookAwsReactNative/ios/Podfile and add the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source &apos;https://github.com/CocoaPods/Specs.git&apos;
pod &apos;React&apos;, :subspecs =&amp;gt; [&apos;Core&apos;, &apos;RCTImage&apos;, &apos;RCTNetwork&apos;, &apos;RCTText&apos;, &apos;RCTWebSocket&apos;], :path =&amp;gt; &apos;../node_modules/react-native&apos;
pod &apos;react-native-fbsdkcore&apos;, :path =&amp;gt; &apos;../node_modules/react-native-fbsdkcore&apos;
pod &apos;react-native-fbsdklogin&apos;, :path =&amp;gt; &apos;../node_modules/react-native-fbsdklogin&apos;
pod &apos;react-native-fbsdkshare&apos;, :path =&amp;gt; &apos;../node_modules/react-native-fbsdkshare&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You &lt;em&gt;may&lt;/em&gt; need to perform some other steps here, as per the &lt;a href=&quot;https://github.com/facebook/react-native-fbsdk#option-using-cocoapods&quot;&gt;Facebook documentation&lt;/a&gt;. Go and have a read of that section just in case. Let’s install the pods and then we’ll have to faff around in Xcode for a bit.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pod install
open FacebookAwsReactNative.xcworkspace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that we now use the xcworkspace file rather than the xcodeproj file; this is required for CocoaPods to do its thing.&lt;/p&gt;

&lt;h2 id=&quot;xcode-is-quite-horrible-really&quot;&gt;Xcode Is Quite Horrible Really&lt;/h2&gt;

&lt;p&gt;Expand the FacebookAwsReactNative &amp;gt; Libraries folder and select all of the items in there and delete the references to them. This is because we’ve opted to use CocoaPods for React Native (see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pod &apos;React&apos;&lt;/code&gt; line we added to the Podfile) so the items in Libraries are duplicates.&lt;/p&gt;

&lt;p&gt;Let’s move on to configuring the Facebook SDK. Open up info.plist using Open As &amp;gt; Source Code, and add the follow inside the root &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;dict&amp;gt;&lt;/code&gt; node:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;CFBundleURLTypes&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;array&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;dict&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;CFBundleURLSchemes&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;array&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;fb{your-app-id}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/array&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/dict&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/array&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;FacebookAppID&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;{your-app-id}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;FacebookDisplayName&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;{your-app-name}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, open AppDelegate.m and add a new import:&lt;/p&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#import &amp;lt;FBSDKCoreKit/FBSDKCoreKit.h&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Connect our AppDelegate to the FBSDKApplicationDelegate by adding the following to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;didFinishLaunchingWithOptions&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FBSDKApplicationDelegate&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sharedInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;didFinishLaunchingWithOptions:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;launchOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And finally, add a method to allow us to navigate back to our app from Facebook:&lt;/p&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BOOL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UIApplication&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;application&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;openURL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSURL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sourceApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;sourceApplication&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;annotation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;annotation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FBSDKApplicationDelegate&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sharedInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;openURL:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;sourceApplication:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sourceApplication&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;annotation:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;annotation&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wonderful. Run the project and get your simulator running because we’re now ready to add our login button.&lt;/p&gt;

&lt;h2 id=&quot;push-the-button&quot;&gt;Push The Button&lt;/h2&gt;

&lt;p&gt;We need to import the button component, and at the same time we’ll import another class that we’ll use later. Add the following:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;FBSDKAccessToken&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;react-native-fbsdkcore&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;FBSDKLoginButton&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;react-native-fbsdklogin&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Delete all of the default stuff inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;View&amp;gt;&lt;/code&gt; component and replace it with the button code:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FBSDKLoginButton&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;onLoginFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onLoginFinished&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;onLogoutFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onLogoutFinished&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;readPermissions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;publishPermissions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;publish_actions&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We now need to add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onLoginFinished&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onLogoutFinished&lt;/code&gt; methods to our FacebookAwsReactNative class.&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;onLogoutFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Logged out.&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Simple!&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;onLoginFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Error logging in.&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isCancelled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Login cancelled.&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Login success!&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Less simple, but pretty self-explanatory. At this point, you should be able to run the app and login via Facebook, with the various alert messages firing as appropriate.&lt;/p&gt;

&lt;p&gt;But we want to take one more step; we want to be able to link this login to persistent data stored on Amazon Cognito. This needs two steps: firstly to get a unique token to identify the Facebook login, and secondly, to sync local data up to Cognito. Fortunately this becomes super-easy thanks to the Amazon SDK and the &lt;a href=&quot;https://github.com/morcmarc/react-native-cognito&quot;&gt;React Native Cognito&lt;/a&gt; project.&lt;/p&gt;

&lt;p&gt;First, we need to get an access token from Facebook to use as a link between FB and Cognito. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FBSDKAccessToken.getCurrentAccessToken&lt;/code&gt; takes a single callback argument which is passed the token we need. We then need to install (follow the &lt;a href=&quot;https://github.com/morcmarc/react-native-cognito&quot;&gt;instructions&lt;/a&gt;) and import React Native Cognito. Add this along with your other import statements:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Cognito&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;react-native-cognito&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, we initialise the credentials for Cognito and sync up some data!&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;FBSDKAccessToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getCurrentAccessToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;Cognito&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;initCredentialsProvider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;awsIdentityPoolId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tokenString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;awsRegion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nx&quot;&gt;Cognito&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;syncData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;testDataset&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// success!&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’ll need to have created a Federated Identity Pool and set up Facebook as an Authentication Provider. Populate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;awsIdentityPoolId&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;awsRegion&lt;/code&gt; variables accordingly and the above code can be run at any point after a successful login.&lt;/p&gt;

&lt;p&gt;Visit the Identity Pool in the AWS Console and you’ll see a dataset called “testDataset” with a single key/value entry.&lt;/p&gt;

&lt;p&gt;This has been a whistlestop tour on moving to a serverless setup with Facebook logins and Amazon Cognito. The full code &lt;a href=&quot;https://github.com/colinramsay/react-native-cognito-example&quot;&gt;can be found on GitHub&lt;/a&gt;.&lt;/p&gt;
</description>
				<pubDate>Fri, 29 Apr 2016 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2016/04/29/go-serverless.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2016/04/29/go-serverless.html</guid>
			</item>
		
			<item>
				<title>React Native - Event Emitters</title>
				<description>&lt;p&gt;Events are a great way of communicating between components, but they should be used carefully to avoid creating an unfathomable mess of interconnections. In this example, we’ll show how to use an event emitter to allow a React Native NavigatorIOS to talk to its child components.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;use strict&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;react-native&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;EventEmitter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;EventEmitter&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Subscribable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Subscribable&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;AppRegistry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;StyleSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;NavigatorIOS&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;First we pull in all of our requirements including the EventEmitter and Subscribable.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;componentWillMount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;eventEmitter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;EventEmitter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;onRightButtonPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;eventEmitter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;myRightBtnEvent&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;someArg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;argValue&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;NavigatorIOS&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;styles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;initialRoute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{{&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Test&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;rightButtonTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Change String&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;onRightButtonPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onRightButtonPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;na&quot;&gt;passProps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;na&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;eventEmitter&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In our main top-level component, we create a new EventEmitter (in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentWillMount&lt;/code&gt;) to be available across the component, and then use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passProps&lt;/code&gt; to pass it down to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Test&lt;/code&gt; component we specify for the navigator.&lt;/p&gt;

&lt;p&gt;We also define a handler for the right button press, which emits a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myRightBtnEvent&lt;/code&gt; with some dummy arguments when that button is pressed. Now, in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Test&lt;/code&gt; child component:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;mixins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Subscribable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Mixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;getInitialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;original string&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;componentDidMount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addListenerOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;myRightBtnEvent&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;miscFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;

    &lt;span class=&quot;na&quot;&gt;miscFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;someArg&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;styles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/Text&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Subscribable&lt;/code&gt; mixin, and the only other thing we need to do is listen out for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myRightBtnEvent&lt;/code&gt; being fired from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App&lt;/code&gt; component and hook &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;miscFunction&lt;/code&gt; up to it. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;miscFunction&lt;/code&gt; will be passed the dummy arguments from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App&lt;/code&gt; press handler so we can use those to set state or perform other actions.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://rnplay.org/apps/H5mMNQ&quot;&gt;You can see a working version of this on RNPlay&lt;/a&gt; and the &lt;a href=&quot;https://github.com/colinramsay/react-native-event-emitters&quot;&gt;source code can be found on Github&lt;/a&gt;.&lt;/p&gt;
</description>
				<pubDate>Sat, 04 Jul 2015 00:00:00 +0000</pubDate>
				<link>http://colinramsay.co.uk/2015/07/04/react-native-eventemitters.html</link>
				<guid isPermaLink="true">http://colinramsay.co.uk/2015/07/04/react-native-eventemitters.html</guid>
			</item>
		
	</channel>
</rss>