While there are many benefits to web applications, they aren't always the best option. What if you want all the perks of a desktop app, but also want to make it easy to use and share? Here's a quick introduction to WebAssembly and why it's so useful.
Recently, more and more everyday tools have been created as web applications. Even if you choose to download the desktop version of an application like Slack, you get a web application that has been bundled with a browser. For performance, this isn’t really great: it’s good enough for most cases but they are hogging more system resources than they should.
That’s why native desktop applications do still have a place, especially for applications where you:
- Expect people to have them open in the background all day.
- Have high performance requirements such as games where you want to make full use of the hardware you have.
At the same time, you don’t want everything to be a native desktop application. There are good reasons that things are moving towards the web, especially in the context of games:
- From a security point of view, people are rightly suspicious of installing random unknown programs on their computers.
- You generally want to get strangers playing your game and getting them to download and install it can be difficult. Being able to run your game in a web browser without installing anything makes getting started and getting new players on board a lot easier.
The web as a platform also makes it much easier to share content. There are many games that run in web browsers. They're one of the easiest types of games to share, since I can drop a link into an article like this, and just by clicking the link, you could be playing with my pomeranian to let my pug take a nap!
So what is WebAssembly and why is it cool?
WebAssembly is a compile target for running native code embedded in a web browser - essentially an assembly language but for the web. This:
- Enables native desktop applications to also be shared in a web browser.
- Allows you to write higher performing code more easily, for example with Rust.
- You can’t just run your code through a compiler and have it produce WebAssembly that magically works. You might have to make some architectural changes to fit your application into the web browser ecosystem.
- You won't be able to just write an infinite game loop like you would on desktop, you need to rely on callbacks into your WebAssembly code from the web browser.
- You also may need to handle different performance characteristics between web and native, like assets being on the other side of a network rather than on the local hard drive.
Important things to know about WebAssembly
- WebAssembly is available for all the major modern web browsers, such as Firefox, Chrome and Edge. No, Internet Explorer is not a modern web browser. This means you can use WebAssembly in most situations, but if you’re working with large slow-moving corporates, you might be blocked by their outdated IT infrastructure.
- The WebAssembly standard is essentially still a minimum viable product. It has enough features to be viable, but some high level features, like multithreading, are only planned for a future release.
- At the moment, if you want to take your Rust, C or C++ code to WebAssembly, the support from those communities is pretty good. The support in many other languages like Typescript, Haskell, Kotlin, C# and Java however is still quite experimental.
- Some of those languages require garbage collection, which still hasn’t arrived yet. Be aware that the WebAssembly files for some of these languages might be large as they bundle in their entire runtime.
- Rust is gearing up to be the premium language for writing WebAssembly, its tooling still needs to mature though.
If you want to hear more about this from me, you can:
- Watch my talk about using WebAssembly for browser games that I gave at Jozi.JS:
- Watch my talk about using WebAssembly to take a Rust application and embed it in a website from DevConf:
- Read an article I wrote about getting started with WebAssembly using Rust
Justin Worthe is a software engineer with an interest in music, games, good coffee, and using programming to get stuff done. He can often be found trying to find ways to play with all of these interests simultaneously. In the meantime, you can catch him on Twitter or Github.