Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

[SF Ruby, March 2024] Rails on Wasm

[SF Ruby, March 2024] Rails on Wasm

Let's push the boundaries and open new horizons for Ruby and Rails applications via WebAssembly.

Vladimir Dementyev

March 28, 2024
Tweet

More Decks by Vladimir Dementyev

Other Decks in Programming

Transcript

  1. Ruby on Rails A web-app framework that includes everything needed

    to create database- backed web applications
  2. Ruby on Rails A web-app framework that includes everything needed

    to create database- backed web applications WebAssembly (Wasm) A binary instruction format for a stack- based virtual machine
  3. Ruby on Rails A web-app framework that includes everything needed

    to create database- backed web applications WebAssembly (Wasm) A binary instruction format for a stack- based virtual machine 🤔
  4. 3

  5. 3 WebAssembly, often shortened to Wasm, is a cutting-edge tech

    that lets you run code super fast in your web browser, almost as if it was running directly on your computer’s hardware. The cool part? It’s not just for one programming language. You can write your stuff in languages like C, C++, Rust, or even Python, then compile it into WASM, and boom, it runs in the web browser. Why it's a big deal: • Speed: It's blazing fast, making it perfect for heavy-duty tasks like games or complex simulations right in your browser. • Versatility: Supports multiple languages, so you're not stuck with JavaScript for web development. • Safety: Runs in a sandbox environment in your browser, keeping things secure. • Ef fi ciency: With its compact binary format, it's quick to load and execute. In short, Wasm is pushing the boundaries of what's possible on the web, making it a game-changer for developers and users alike.
  6. 4

  7. Wasm in a nutshell .c .rs .js .rb .py x86

    ARM machine code machine code Wasm runtime (VM)
  8. 8

  9. 9

  10. Ruby on Wasm • Browser-based interactive playgrounds • Edge/serverless computing

    • Embedded scripting engine for platforms/applications • Portable Ruby libraries/applications (prism.wasm*) What's the point?
  11. ruby.wasm demo • Let's bring Coltrane online! • Get our

    hands dirty with ruby.wasm and rbwasm build • Face the limitations of Ruby on Wasm github.com/pedrozath/coltrane 🎷
  12. 19

  13. Rails on Wasm • (Many) Dependencies with C extensions (nokogiri

    😈) • Databases (Active Record) Rails != Ruby
  14. Rails on Wasm • (Many) Dependencies with C extensions (nokogiri

    😈) • Databases (Active Record) • HTTP server Rails != Ruby
  15. Rails on Wasm • Threads and outgoing HTTP • Assets

    and uploads (Active Storage) Rails != Ruby
  16. Rails on Wasm • Threads and outgoing HTTP • Assets

    and uploads (Active Storage) • Background jobs Rails != Ruby
  17. Rails on Wasm • Threads and outgoing HTTP • Assets

    and uploads (Active Storage) • Background jobs • Real-time (Action Cable) Rails != Ruby
  18. Rails on Wasm: Databases • SQLite3 and PostgreSQL (pglite) available

    in Wasm • Persistence via OPFS (future) or Indexeddb (legacy)
  19. Rails on Wasm: Databases • SQLite3 and PostgreSQL (pglite) available

    in Wasm • Persistence via OPFS (future) or Indexeddb (legacy) • Not targeting browsers? Kein Problem!
  20. Rails on Wasm: HTTP Server • ServiceWorker for browsers •

    Simple HTTP proxy for other runtimes • wasi:http/proxy for plug-n-play DX in the future
  21. Rails on Wasm: Background Jobs • We can run Postgres

    on Wasm—Good Job! • Separate Worker(-s) + BroadcastChannel
  22. Rails on Wasm: HTTP • HTTP via fetch (see Faraday::JS

    in RubRuby.dev) • wasi/http (preview 3) in the future
  23. 30

  24. Rails on Wasm • TLR = Try-Learn-Reproduce • Personal/private applications

    (own your data) • Local- fi rst desktop apps (e.g., music libraries, notes, Trello-like boards) What's the case?
  25. Rails on Wasm • Multi-env apps: same code for web,

    CLI, etc. • Offline- fi rst 2.0: fully offline execution, data snapshots and replication What's the case?
  26. Rails on What? • JS runtimes (browsers, Node.js, Bun, Deno)

    • Serverless • Desktop (Tauri, Wails) • CLI (via Wazero/Wasmedge/etc)
  27. Rails on Wasm • WASI Components • WASI Preview 2

    (HTTP) • More compatible extensions (nokogiri.wasm) What's the future?
  28. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI pglite
  29. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI We are here pglite
  30. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI ruby.wasm WASI Components We are here pglite
  31. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI ruby.wasm WASI Components ruby.wasm WASI Preview 2 We are here pglite
  32. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI ruby.wasm WASI Components ruby.wasm WASI Preview 2 We are here pglite WASI Preview 3
  33. Rails on Wasm 2013 2017 2019 2022 2024 ??? ???

    ??? ??? WebAssembly asm.js SQLite3 WASM Ruby 3.2 WASI support RubRuby.dev Rails on Wasm (SF Ruby Meetup) WASI ruby.wasm WASI Components ruby.wasm WASI Preview 2 We are here pglite bin/rails pack:wasm WASI Preview 3