Ports in Elm


I recently finished the basic Elm tutorial that Tenor introduced me to. The last assignment was on ports.   This to me seemed very strange. What is an elm port?  Why do we need it? It reminded me of this this Rust WASM feature request:

Request for library: mpsc channels library built on top of the postMessage API #163

This could allow wasm to talk to js via CSP style message passing, different wasm instances to talk to each other without sharing memory, the wasm instances might even be in different threads by using web workers.

So what are Elm ports? Ports are the way that javascript and Elm talk to each other.  In Elm ports, messages are one way. It is not like http or tcp/ip where there is a handshake. I can send a message from JS to Elm, and vice versa, but I do not confirm that the message was received.

It is a pub/sub pattern, where JS subscribes to a port that is populated in Elm, and then sends messages on a port that is then handled in elm.

I did not expect Elm to work this way, but its not that strange. Maybe I can use some concepts and apply them to RUST WASM.

WASM is Magic


A few weeks ago I stumbled upon wasm when I was

  1. Looking for Rust projects to contribute to
  2. Looking for a way to write smart contract that was not in javascript

I came upon wasm for both of these things. So what is WASM? WASM is web assembly. It is a binary format for web application.   You can write an application in Rust, or C, or any language that compiles into web assembly and then load it into a web page. The benefit is near native performance (and not having to use Javascript).   There are also some security benefits to the way that web assembly memory is allocated – the execution stack is separated from the WASM stack. Here is a whole medium piece  someone wrote on JS and memory if you are interested.

I found it very magical writing something in rust and then publishing it as a node package and calling functions in another js function.

There are two resources I looked at for web assembly. The web assembly book https://rustwasm.github.io/book and https://rustwasm.github.io/book/game-of-life/implementing.html. I recommend the second one because it has a really interesting example of canvas integration with WASM. There is also a podcast on this week in rust about WASM.   AND just because WASM was written for the web, and has ‘web’ in the name does not mean it could not be used for other things. It could be like a better type of virtual machine (think JVM) or something. Some other good links are the awesome rust and wasm page and wasm-bindgen examples.  I also heard about an open frameworks-ish library in WASM from Max D.

It also made me think about the way JS communicates to Non JS. So JS and WASM, or JS and elm (ports). I am not sure why this is interesting, but there is something about this sort of interop that I find worth thinking about it.

I still have some more work to do on my personal Rust WASM project, and am going to work with Marko on a Rust WASM version of his breakout speed analysis comparison. I was going to put a link to the heroku deploy – but because of the rust module this is not trivial. I think I need to write a heroku build pack or publish the package to npm. But here is a screen cap – its in the browser !

This is unrelated to WASM, but J has a great blog post about editing our git history. This is super helpful since I sometimes get lazy on personal projects and commit with unhelpful messages. My main git trick is bfg, when I want to remove something that I should not have committed…. It happens.