<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kuba Gonet on Medium]]></title>
        <description><![CDATA[Stories by Kuba Gonet on Medium]]></description>
        <link>https://medium.com/@jakub.gonet?source=rss-8aa528e2d93a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TFXjaPZZHwb2SlBY</url>
            <title>Stories by Kuba Gonet on Medium</title>
            <link>https://medium.com/@jakub.gonet?source=rss-8aa528e2d93a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:09:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jakub.gonet/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Three-Lang Problem: shipping Elixir runtime, JS, and WebAssembly as one npm package]]></title>
            <link>https://medium.com/swmansion/the-three-lang-problem-shipping-elixir-runtime-js-and-webassembly-as-one-npm-package-5a7b76ad5b90?source=rss-8aa528e2d93a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a7b76ad5b90</guid>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Kuba Gonet]]></dc:creator>
            <pubDate>Thu, 12 Mar 2026 15:03:51 GMT</pubDate>
            <atom:updated>2026-03-12T15:03:51.640Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Three-Lang Problem: Shipping Elixir Runtime, JS, and WebAssembly as One npm Package</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V2WbxYhsDqA5hIAdcM2kfg.png" /></figure><p><a href="https://popcorn.swmansion.com/">Popcorn</a> is a library we develop that lets you run Elixir in the browser (zip-ties and glue included!). As you might guess, it has a bit more convoluted setup than a typical Elixir or JS library.</p><p>For a long while, using Popcorn meant copying two JS files from our template into your project as well as installing the popcorn Hex package for Elixir. The only advantage of copying was simplicity of development (for us). <br>A lot of projects use Phoenix which already includes a way to install a JS package. Other projects, like our Elixir language tour are React apps. Integrating via npm install is way easier than manually tracking JS files.</p><p>And frankly, it’s weird we had an official Elixir package and hacks for JS parts.</p><p>We decided to <em>just</em> extract those files into an npm library and make setup simpler for users. It didn’t come easy. You may even say it came with unrelenting perversity of inanimate objects.</p><h3>The Architecture™</h3><p>The main problem is that our architecture choices force us to place JS and Wasm files in carefully chosen places, with carefully chosen filenames. It mainly comes from our use of Emscripten.</p><p>A few words on the tech stack — we use AtomVM, which is a BEAM runtime written in C.<br>It compiles to Wasm via Emscripten, generating a <em>.wasm</em> file and <em>.mjs</em> JS glue code. We take them and throw them into an iframe (for <a href="https://x.com/JakubGonet/status/2028875935106109520">isolation</a>). Then it spawns the WebWorkers it needs to emulate threads.<br>End-user code communicates with it by exchanging messages back and forth with the iframe.</p><p>Here’s how it looks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A0x72oHEDkMI3s9lTACPGw.png" /></figure><p>We have one file with the main script that has access to the DOM, another file in an iframe that loads and isolates AtomVM (and has access to the DOM via parent context), and many WebWorkers that load AtomVM directly (but incidentally <strong>use the same file</strong> that is loaded in the iframe).</p><p>It works beautifully until you start poking it:<br>- the iframe has a different view of the base URL than the main JS context.<br>- bundlers don’t have very good support for Wasm.<br>- WebWorkers with shared memory need CORS/COEP headers set by the server.<br>- Emscripten by default is configured to use one instance of the Wasm module and isn’t afraid of global variables.<br>- it also generates the JS in a way that hardcodes the JS glue filename.</p><p>Given all that, we have two requirements for the library:<br>- we want <em>iframe.js</em> to be a separate entrypoint that resolves <em>AtomVM.mjs</em> correctly.<br>- we want <em>AtomVM.mjs</em> to find its Wasm file (both in iframe and WebWorker contexts).</p><h3>Stubbing Your Toe on Imports</h3><p>I mentioned bundlers. For non-frontend developers, these are tools that take your Javascript project and output a single file containing all the JS code. Two popular choices are Esbuild and Rollup. To my complete astonishment, they don’t really care about Wasm and treat it as any other (binary) asset.<br>This is great, since you can have the unique experience of reviewing how different bundlers handle totally nonstandard ways of importing and using Wasm files.</p><p>I wanted to mark generated files as external modules (which means that they aren’t included in the main JS bundle (which means that they keep their filenames)).<br>This made sense to me at the time but had unfortunate implications: <em>iframe.ts</em> entrypoint which imported the external AtomVM module wasn’t resolving the import correctly because entrypoints are controlled by end-user’s code and we need them to be specifically next to <em>AtomVM.mjs</em> script. This would impact how users structure their projects.</p><p>The logical way of fixing that is to either change <em>AtomVM.mjs</em> to be bundler-friendly (removing the need to handle this file specially) or provide a plugin for users that configures it correctly. I really wanted to avoid the second one since it meant more setup for end-users and possibly more things that can break when you configure your bundler in a non-standard way.</p><p>Here are two fun excerpts from Emscripten-generated JS code.</p><h4>Exhibit One:</h4><pre>function findWasmBinary() {<br>  if (Module[&quot;locateFile&quot;]) {<br>    return locateFile(&quot;AtomVM.wasm&quot;);<br>  }<br>  // Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.<br>  return new URL(&quot;AtomVM.wasm&quot;, import.meta.url).href;<br>}</pre><h4>Exhibit Two:</h4><pre>worker = new Worker(new URL(&quot;AtomVM.mjs&quot;, import.meta.url), {...});</pre><p><em>Long story short</em>, Emscripten really wants you to keep the generated filenames unchanged and place them in the same directory.<br>Bundlers, on the other hand, really want to keep everything in the same bundle (and change the filenames to include content hash).<br>This was the second signal to try to improve <em>AtomVM.mjs</em> code to make it bundle-able.</p><blockquote><strong>Small note: WebWorkers</strong></blockquote><blockquote>There are two main ways to use your code in WebWorkers. You can either provide a link to a script, similar to how HTML imports scripts or you can pass the JS string with script on creation (this approach is deprecated to my knowledge). I want to emphasize that <strong>both iframe and WebWorkers create new JS contexts</strong> — you don’t have access to variables or loaded scripts across them. If you need anything (a function, a variable, an entire library), it needs to be included in the JS code passed to the WebWorker.</blockquote><p>Emscripten has an option to remove the need for a separate <em>.wasm</em> file by including the binary blob in the script, in the form of a base64 encoded string (with a <em>mere</em> 33% size increase due to encoding).<br>WebWorkers need a separate script to load and bundlers have an option for that. This wouldn’t be a problem if we didn’t have the same script that is loaded in both the main browser context and WebWorkers, behaving differently in each.</p><p>We can remove the hardcoded <em>.wasm</em> name by using base64.<br>Now we need to remove the import for the script itself when inside WebWorker context. Well, what if we just pack <strong>the entire script into base64 string</strong> (which <strong><em>includes</em></strong> base64-packed VM code)?</p><p>Some thoughts shouldn’t be thought.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0f0eGP2NCTeqbsIOwi6qjg.png" /><figcaption>Such a great idea, let’s write that one down.</figcaption></figure><h3>Plugins</h3><p>At this point, I was three weeks into research, mostly reading generated code, and the Emscripten and bundler docs. Trying to eliminate Wasm files or working around Emscripten looked like a dead-end.<br>I came back to the original idea of wrapping the the necessary logic into a plugin that will integrate with the user’s project.</p><p>This has numerous advantages compared to the previous attempt such as: not losing your sanity.</p><p>I also realized after many plugin drafts that we don’t need any sophisticated bundler features, it’s enough to put our files in the output directory and make sure bundlers don’t touch them.<br>In other words, we came back to <em>cp template.js actual-file.js</em> but with one additional (and useful!) abstraction. This allows us to have extremely simple plugins for both and to integrate with Vite, too (which uses both under the hood).</p><p>Full workflow: we develop Popcorn locally with TypeScript and other tools, we then ship both our custom code (API and plugins) and AtomVM compiled via Emscripten, and with some strategic file placement and hooking into the user’s build pipeline we force all of that to work.</p><p>This solves both of our previously stated constraints:</p><blockquote>We want iframe.js to be a separate entrypoint that resolves AtomVM.mjs correctly.</blockquote><p>Popcorn ships source files, which we then copy. <em>iframe.js</em> is a sibling file of <em>AtomVM.mjs</em>, no special handling needed to resolve imports.</p><blockquote>We want AtomVM.mjs to find its Wasm file (both in iframe and WebWorker contexts).</blockquote><p>Same as above. We also don’t modify the filenames so the generated Emscripten code always refers to files that exist (and not, for example, have a content hash appended to its name).</p><p>We’ve been using this approach in the Elixir language tour for a while now, and it works nicely — <a href="https://github.com/software-mansion/popcorn/pull/489">PR</a> replacing the old implementation was satisfyingly red.</p><p>Best part of the diff:</p><pre>export default defineConfig({<br>  base: &quot;/&quot;,<br>  plugins: [<br>    react(),<br> // ...<br>    svgr(),<br>+   popcorn({ bundlePath: &quot;./public/wasm/bundle.avm&quot; }),<br>    cookOnChange()<br>  ],</pre><p>It simplifies the developer experience a lot, with both JS-first projects and Elixir-first projects.</p><h3>Packing It All Up</h3><p>It’s available to use now. You’ll need the <a href="http://twitter.com/swmansion/popcorn"><em>@swmansion/popcorn</em></a> npm package and <em>popcorn</em> Hex package. We have <a href="https://github.com/software-mansion/popcorn/tree/main/examples">examples</a> to show how it works (especially <em>hello-popcorn</em> with an Elixir-first approach and <em>hello-react</em> with a JS-first approach).</p><p>Since the last release (which was ages ago), we’ve also worked a lot on improving AtomVM and our use of it. You can find a changelog in <a href="https://github.com/software-mansion/popcorn/releases">Github releases</a>.</p><p>We also saw some really cool demos: Steffen’s <a href="https://popcorn-lv.steffend.me/">unmodified LiveView working in the browser</a> and Peter’s <a href="https://petermm.github.io/popcorn_live_view/req-demo">collection of small demos</a> (building on Steffen’s site, I especially like the sqlite one).</p><p>We’re also cooking something in the shadows — <a href="https://youtu.be/CTAud5O7Qqk">float on</a> until that time. It may be slightly interesting.</p><p>Take care,<br>Kuba!</p><p>We are Software Mansion — Elixir experts, AI explorers, React Native core contributors, community builders, and software development consultants. Need help with building your next product? You can <a href="https://swmansion.com/contact?utm_source=medium&amp;utm_medium=display">hire us</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a7b76ad5b90" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swmansion/the-three-lang-problem-shipping-elixir-runtime-js-and-webassembly-as-one-npm-package-5a7b76ad5b90">The Three-Lang Problem: shipping Elixir runtime, JS, and WebAssembly as one npm package</a> was originally published in <a href="https://medium.com/swmansion">Software Mansion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Not all who wander are lost]]></title>
            <link>https://medium.com/swmansion/not-all-who-wander-are-lost-805811c433c5?source=rss-8aa528e2d93a------2</link>
            <guid isPermaLink="false">https://medium.com/p/805811c433c5</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[expo]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kuba Gonet]]></dc:creator>
            <pubDate>Thu, 28 Mar 2024 10:37:25 GMT</pubDate>
            <atom:updated>2024-03-28T10:37:25.483Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jppRDMihw8SFkRX6MC6bOA.png" /></figure><p>Expo Location is a library that allows you to access the geolocation of the device. In particular, it can access the location when the application runs in the background. It’s often used to create navigation applications (like Google Maps) or fitness tracking applications (e.g. Strava).</p><p>We’ll guide you through the steps required to set up and use the background location in an Expo app.</p><h3>Setup</h3><p>First of all, we need to install expo-location, alongside with expo-task-manager which we’ll use to keep the location running in a background task.</p><pre>npx expo install expo-location expo-task-manager</pre><p>The background location requires additional permission requests, which are configured in the Info.plist for iOS and AndroidManifest.xml for Android.</p><p>In an Expo project, you configure these settings in the app.json file, in the section for additional configuration for plugins.</p><pre>// app.json<br>// …<br>“plugins”: [<br>  [<br>    “expo-location”,<br>    {<br>      “isIosBackgroundLocationEnabled”: true,<br>      “isAndroidBackgroundLocationEnabled”: true<br>    }<br>  ]<br>],<br>// …</pre><h3>Permission request and task registering</h3><p>Next, we need to ask the user to authorize location tracking. For background location, we need the “Always allow” option to be enabled.</p><p>This is done in two steps — ask for foreground location permission and then upgrade the permission to background location.</p><pre>async function requestPermissions() {<br>  const requestForeground = Location.requestForegroundPermissionsAsync;<br>  const requestBackground = Location.requestBackgroundPermissionsAsync;<br><br>  const foregroundRequest = await requestForeground();<br>  if (foregroundRequest.granted) {<br>    const backgroundRequest = await requestBackground();<br>    if (backgroundRequest.granted) {<br>      return true<br>    }<br>  }<br>  return false<br>}</pre><p>After doing that, we can register the task:</p><pre>const LOCATION_TASK_NAME = “background-location-task”;<br>await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {<br>  accuracy: Location.Accuracy.Highest,<br>  activityType: Location.LocationActivityType.Fitness,<br>});</pre><p>We can customize many options, e.g. time after a batch of updates will be delivered to the callback, location accuracy, etc. You can learn about all the options from the Expo Location documentation <a href="https://docs.expo.dev/versions/latest/sdk/location/#locationtaskoptions">[1</a>], <a href="https://docs.expo.dev/versions/latest/sdk/location/#locationoptions">[2]</a>.</p><p>Lastly, we need to register a callback with the same name to be called when the background location update arrives. The callback must be fairly fast to preserve the battery and to fit in platform’s time constraints (e.g. on iOS background service must take at most 30s).</p><pre>TaskManager.defineTask(LOCATION_TASK_NAME, saveLocationDataTask);</pre><p>saveLocationDataTask is a callback that takes one argument, an object with data and error keys. The data has a following shape:</p><pre>type FullLocationData = {<br>  coords: {<br>    altitude: number;<br>    altitudeAccuracy: number;<br>    latitude: number;<br>    accuracy: number;<br>    longitude: number;<br>    heading: number;<br>    speed: number;<br>  };<br>  timestamp: number;<br>};</pre><p>In our example, we’re taking latitude, longitude, and accuracy and then saving it using async storage.</p><p>After adding mapbox and creating a visualisation…</p><pre>function Map() {<br>  const locations = useLocationData();<br>  if (locations.length === 0) {<br>    // …<br>  }<br>  return (<br>    &lt;MapView style={styles.fill} deselectAnnotationOnTap={true}&gt;<br>      &lt;ShapeSource id=&quot;routeSource&quot; shape={toRoute(locations)}&gt;<br>        &lt;LineLayer id=&quot;routeFill&quot; style={mapStyles.lineStyle} /&gt;<br>      &lt;/ShapeSource&gt;<br>      &lt;Camera defaultSettings={cameraConfig(locations[0])} /&gt;<br>    &lt;/MapView&gt;<br>  );<br>}<br><br>function toRoute(locations: LocationData[]): LineString {<br>  return { type: &quot;LineString&quot;, coordinates: locations.map(toCoords) };<br>}<br><br>function cameraConfig(initialLocation: LocationData) {<br>  return { centerCoordinate: toCoords(initialLocation), zoomLevel: 16 };<br>}</pre><p>…we’re ready to supply the data. The easiest way is to use location emulation included in iOS simulator and Android emulator or using a fake GPS app on Android.</p><p>If you’re feeling adventurous, you can go outside for a walk.</p><p>Here’s a short route recorded around the Software Mansion office.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*gqpBeKfoDzbdWGHytJMk3g.jpeg" /><figcaption>A route recorded by the app</figcaption></figure><p>One thing to take note of is that some Android devices such as Huawei ones have strict battery policy — this means that even with background location enabled, the app may not receive updates. Based on <a href="https://stackoverflow.com/questions/55260013/huawei-android-devices-properly-requesting-permissions-for-background-location">this StackOverflow answer</a>, it most likely can be circumvented by disabling battery optimisations for the app.</p><p><a href="https://github.com/jakub-gonet/swm-background-location">Repository link with full example.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=805811c433c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swmansion/not-all-who-wander-are-lost-805811c433c5">Not all who wander are lost</a> was originally published in <a href="https://medium.com/swmansion">Software Mansion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>