<?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: Ayomide Onigbinde</title>
    <description>The latest articles on Forem by Ayomide Onigbinde (@oayomide).</description>
    <link>https://forem.com/oayomide</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%2F171116%2F799018c4-c8b7-451b-80d9-53e69b985d34.png</url>
      <title>Forem: Ayomide Onigbinde</title>
      <link>https://forem.com/oayomide</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/oayomide"/>
    <language>en</language>
    <item>
      <title>WebAssembly and the frontend: End of JavaScript?</title>
      <dc:creator>Ayomide Onigbinde</dc:creator>
      <pubDate>Thu, 10 Dec 2020 14:28:36 +0000</pubDate>
      <link>https://forem.com/oayomide/webassembly-and-the-frontend-end-of-javascript-28o</link>
      <guid>https://forem.com/oayomide/webassembly-and-the-frontend-end-of-javascript-28o</guid>
      <description>&lt;p&gt;Frontend development is awesome. JavaScript has gotten faster, we can now write code that run in the browser that run on the GPU. But what if we can do more? What if we want to really run backend code in the browser?&lt;/p&gt;

&lt;p&gt;Before we go on, let me clarify that &lt;strong&gt;WebAssembly is not going to replace JavaScript. As it stands right now, you actually still need JavaScript to access the DOM from WebAssembly because wasm cannot access the DOM directly (at least right now).&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is WebAssembly (briefly)?
&lt;/h3&gt;

&lt;p&gt;WebAssembly is a binary format that essentially lets you compile code and run this compiled code both on the client and the server – well, ANYWHERE really. It bears similarities with the JVM because they're both "RUN ANYWHERE". Please check out &lt;a href="https://dev.to/oayomide/webassembly-another-jvm-4g79"&gt;my (brief) article&lt;/a&gt; on the differences between the JVM and wasm. Also, check &lt;a href="https://stackoverflow.com/questions/58131892/why-the-jvm-cannot-be-used-in-place-of-webassembly"&gt;this stackoverflow link&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  So how does it fit into the frontend
&lt;/h3&gt;

&lt;p&gt;Lets say you have a image compression algorithm you've written in Rust and using it on the server, when you compile it to wasm, it lets you run this same code in the browser thus eliminating need to go through a server while still providing near native speed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wTV225R1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/73tqmp9e3cggi7bcq60s.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wTV225R1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/73tqmp9e3cggi7bcq60s.jpeg" alt="Alt Text" width="880" height="436"&gt;&lt;/a&gt; (source: &lt;a href="https://blog.logrocket.com"&gt;https://blog.logrocket.com&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;The implication here is that more powerful code can be run on the client. Not only does it let you run such code in the client, it does so securely too while maintaining near native speed! Wasm apps run in a sandbox – they cannot access another process on the machine so whatever they're doing is restricted to that specific sandbox.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't I need to be able to write Go, Rust, C and other systems language before I can do wasm?
&lt;/h3&gt;

&lt;p&gt;WebAssembly is an "official web language" like JavaScript – its now HTML, CSS, JavaScript and WebAssembly.&lt;/p&gt;

&lt;p&gt;In my opinion, you don't need to know a backend or systems language to use wasm. All you need is the WebAssembly binary and you can get to work. In the case where you need to debug the wasm code, you can simply use WAT format – WebAssembly Text; a human readable text representation of your wasm file. Just knowing the functions and similar things the wasm binary exposes is also all that is needed. Once you know the function to call and what parameters to pass, you can just go ahead and do your thing.&lt;/p&gt;

&lt;p&gt;Sure, it'd be nice to have an idea how the code that was compiled to wasm works but you can do just fine using just the wasm file compiled for you.&lt;/p&gt;

&lt;p&gt;Remember, same way you don't necessarily need to understand how the server returns data when you consume an API, same way you don't need to know a systems/backend language to use WebAssembly in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Are there people using WebAssembly in the browser already?
&lt;/h3&gt;

&lt;p&gt;Sure! There are a few products already using wasm in production in a browser. &lt;a href="https://figma.com"&gt;Figma&lt;/a&gt; uses it in production, &lt;a href="https://earth.google.com"&gt;Google Earth&lt;/a&gt;, &lt;a href="https://squoosh.app"&gt;Squoosh&lt;/a&gt; and self plug: &lt;a href="https://yaimp.herokuapp.com"&gt;me too!&lt;/a&gt; This list is not exhaustive, there are more people adopting WebAssembly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The sky is limit when you can run more performance intensive code in the browser. WebAssembly lets you save more developer time because one code can run anywhere its needed instead of porting to another language that the target platform supports. It guarantees safety too without sacrificing performance. Its a good day to hop on the wasm train 😊🤙🏾&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YK1BJGBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ugsjxxtawgwxupm9ie2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YK1BJGBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ugsjxxtawgwxupm9ie2o.png" alt="Alt Text" width="880" height="1156"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;WebAssembly files used by Figma in their web app&lt;/em&gt;&lt;br&gt;
(source: brave dev tools; figma)&lt;/p&gt;

&lt;p&gt;In my next article, we're going to look at building an app that runs fully in the browser using WebAssembly.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>WebAssembly: another JVM?</title>
      <dc:creator>Ayomide Onigbinde</dc:creator>
      <pubDate>Wed, 23 Sep 2020 14:18:58 +0000</pubDate>
      <link>https://forem.com/oayomide/webassembly-another-jvm-4g79</link>
      <guid>https://forem.com/oayomide/webassembly-another-jvm-4g79</guid>
      <description>&lt;p&gt;WebAssembly has been gaining popularity in recent times. It's fast, it's "hip" and my God, C/C++ and Rust developers seem to mention it a lot these days. There are several articles already written for Wasm (as I'll keep calling WebAssembly in this article). Go &lt;a href="https://dev.to/oayomide/create-a-webassembly-app-with-react-and-go-39j7"&gt;here&lt;/a&gt; for my article on using WebAssembly, Go and React. There is also a Rust port &lt;a href="https://github.com/oayomide/yaimp-rs"&gt;here&lt;/a&gt;. The Rust port is up and running &lt;a href="https://yaimp.herokuapp.com"&gt;at this URL.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Java is a language that needs no introduction. The JVM is simply a virtual machine that takes in &lt;a href="https://en.wikipedia.org/wiki/Java_bytecode"&gt;Java bytecode&lt;/a&gt; and then spit out the machine code of the CPU the code is running on. That way, you can write any language that is supported by the JVM (e.g Scala) and run it on any CPU/platform. This forms the basis of Java's Write Once, Run Anywehere. Enter Wasm.&lt;/p&gt;

&lt;p&gt;Wasm basically is a format that allows you compile code you've written in various languages on any hardware/platform in a safe environment.&lt;/p&gt;

&lt;p&gt;So how does Wasm differ from the JVM?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed. Wasm runs at near native speed (even in the browser).&lt;/li&gt;
&lt;li&gt;Security. WebAssembly lets you run code you don't trust. It runs in its own sandbox.&lt;/li&gt;
&lt;li&gt;In the frontend, there are JS bindings from your host language. This means you can write your app in a language like Rust. You can manipulate the DOM directly, compile to Wasm and have JS bindings generated for you.&lt;/li&gt;
&lt;li&gt;WebAssembly can be loaded "lazily" loaded. What does this mean? You can actually run the WASM code before its been fully dowloaded. The browser can run the WASM as its being downloaded using the

&lt;code&gt;WebAssembly.instantiateStreaming()&lt;/code&gt;

function provided in JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Personally, my biggest takeaway from the differences between JVM and WebAssembly is that Wasm is platform-independent and provides near native speed for whatever architecture you're running on all with support for many languages, guaranteed security and the fact that you can stream the Wasm binary in the browser. Another takeaway is the fact that it supports a lot of languages — Rust, Go, AssemblyScript all support Wasm. I think its pretty obvious that Wasm is not just another JVM. WebAssembly &lt;strong&gt;is&lt;/strong&gt; WebAssembly.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Create a WebAssembly app with React and Go</title>
      <dc:creator>Ayomide Onigbinde</dc:creator>
      <pubDate>Fri, 06 Sep 2019 01:16:19 +0000</pubDate>
      <link>https://forem.com/oayomide/create-a-webassembly-app-with-react-and-go-39j7</link>
      <guid>https://forem.com/oayomide/create-a-webassembly-app-with-react-and-go-39j7</guid>
      <description>&lt;h1&gt;
  
  
  Getting started with WASM, React and Go
&lt;/h1&gt;

&lt;p&gt;WebAssembly is awesome --barely a news, but how can you use it with React and Go? Well, honestly there are materials out there that are good to help to figure this out. But personally, it wasnt a smooth experience. I wanted a straightforward, up-to-date article. For one, I was looking for a solution that used Create-React-App because who wants to mess with Webpack? 🙃🙃&lt;/p&gt;

&lt;p&gt;The aim of this short and simple tutorial is to show you how I pieced everything together and have a React based Go program compiled to WebAssembly up and running. I'd assume the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have Go installed (1.11 and above).&lt;/li&gt;
&lt;li&gt;Npm/Yarn installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So lets get to it.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll enter a value into the input box in react and then use WebAssembly to render this value. Simple and interesting, right?&lt;/p&gt;

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

&lt;p&gt;Clone the repo &lt;a href="https://github.com/oayomide/go-reactive" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The repo contains the ejected app. I am trying to make this article as brief as possible so I might (unintentionally) miss some things.&lt;/p&gt;

&lt;p&gt;In the root, we have &lt;code&gt;main.go&lt;/code&gt; that contains the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"syscall/js"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;

&lt;span class="c"&gt;// init is called even before main is called. This ensures that as soon as our WebAssembly module is ready in the browser, it runs and prints "Hello, webAssembly!" to the console. It then proceeds to create a new channel. The aim of this channel is to keep our Go app running until we tell it to abort.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, WebAssembly!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// here, we are simply declaring the our function `sayHelloJS` as a global JS function. That means we can call it just like any other JS function.&lt;/span&gt;
    &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sayHelloJS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FuncOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Done.. done.. done..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// tells the channel we created in init() to "stop".&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// SayHello simply set the textContent of our element based on the value it receives (i.e the value from the input box)&lt;/span&gt;
&lt;span class="c"&gt;// the element MUST exist else it'd throw an exception&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsV&lt;/span&gt; &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"document"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"getElementById"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"textContent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our function sayHello takes two arguments. We're more concerned with the second. What the second does essentially is that it takes an array of js.Value. This is because we can pass as many argument as we want from JavaScript. To get the values, we simply use the index. So, in our case, we want to get the value entered in the input box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like I said earlier, we use the index of the array to get the value of whatever we want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"document"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"getElementById"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"textContent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the above code is similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what we're doing is that we're changing the text of our element with the id "message" with the input value.&lt;/p&gt;

&lt;p&gt;Run the following to compile the &lt;code&gt;main.go&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;js &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;wasm go build &lt;span class="nt"&gt;-o&lt;/span&gt; ../client/public/main.wasm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Client side of things
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;App.js&lt;/code&gt;, we have this in the componentDidMount():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instantiateStreaming&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main.wasm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;go&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;importObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;go&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;inst&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;instance&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're instantiating our &lt;code&gt;main.wasm&lt;/code&gt; and running the instance. That means we can now go ahead and call our WASM functions in our app. Also, setting the module and instance to state incase we need them later on. Also, &lt;strong&gt;you'd notice that we're doing &lt;code&gt;window.go.run(instance)&lt;/code&gt;.&lt;/strong&gt; Where did it come from? Well, its already handled in the React app. You'd notice there is a &lt;code&gt;wasmjs&lt;/code&gt; folder which contains an &lt;code&gt;init_js&lt;/code&gt; file. This and the &lt;code&gt;wasm_exec.js&lt;/code&gt; file needed to use our WASM file with JS have been created and bundled with our React App using webpack. &lt;strong&gt;So it binds the Go() instance to the global window. Therefore instead of declaring a new instance of Go(), it exists as a window object variable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayHelloJS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is us calling our function sayHelloJS that we registered in our Go code earlier! Notice we're accessing as a window object property. This is because we're calling it from React. It's going to be undefined if we called it as &lt;code&gt;sayHelloJS&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Ayomide Onigbinde wrote this!!😉...💕 from WebAssembly and Golang
&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have this HTML element. Note that it has an id "message". This element was the one we manipulated in our Go code which we compiled to WebAssembly! So this element must exist else it'd throw an exception. This is what will change into whatever value we enter in the input box! And its WebAssembly (compiled from Go) doing this! 🎉🎉&lt;/p&gt;

&lt;p&gt;I tried to explain as best as I could! Please if there's something not clear, comment and we'd figure it out together 😉😉.&lt;/p&gt;

&lt;p&gt;Huge props to Aaron for his &lt;a href="https://www.aaron-powell.com/posts/2019-02-06-golang-wasm-3-interacting-with-js-from-go/" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; and big big thanks to &lt;a href="https://github.com/Chrischuck/ai-tic-tac-toe/tree/master/client/src" rel="noopener noreferrer"&gt;Chris at LogRocket&lt;/a&gt;. It was a huuuggeee help! &lt;a href="https://dev.to/brightdevs/using-webassembly-with-react-1led"&gt;This Dev.to article&lt;/a&gt; also helped to get an understanding about React with WASM.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webassembly</category>
      <category>go</category>
    </item>
  </channel>
</rss>
