# Swift + HTMX = A New Hope

**URL:** <https://forums.swift.org/t/swift-htmx-a-new-hope/72755>\
**Category:** Community Showcase\
**Tags:** server, web, html\
**Created:** [June 26, 2024, 10:13am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755 "2024-06-26T10:13:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [June 26, 2024, 10:13am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/1 "2024-06-26T10:13:14Z")

</div>

Hello there, lovely swift community!

If you are anything like me, you love swift and strongly dislike javascript (like any reasonable person ; )

However, interactive web apps are essentially not possible without javascript - and more often then not you end up building and bundling fat client-side javascript codebases.

[SwiftWasm](https://swiftwasm.org/) and the inspiring ecosystem emerging around it can be a way out, but for my taste it is not quite ready for load-bearing prime time. Also, not every web app needs "fat" client-side logic.

So, what to do?

When I first heard about [HTMX](https://htmx.org/) I did not think much of it. As it kept popping up I started to realize that it can be a nice way out:

- keeps your application code on the server side (in beautiful swift)
- achieves SPA-like interactivity
- reduces the need for client-side code A LOT
- no all-devouring javascript frontend library required

I am on a mission to make that work well. I think I am very close.

> **[GitHub - sliemeobn/elementary-htmx: HTMX + Elementary: Hypertext web apps...](https://github.com/sliemeobn/elementary-htmx)**
>
> HTMX + Elementary: Hypertext web apps with Swift. Contribute to sliemeobn/elementary-htmx development by creating an account on GitHub.

Check out the examples (both with auto-reload on save):  
[Hummingbird example app](https://github.com/sliemeobn/elementary-htmx/tree/main/Examples/HummingbirdDemo)  
[Vapor example app](https://github.com/sliemeobn/elementary-htmx/tree/main/Examples/VaporDemo)

Let me know what you think!

---

<div class="post-metadata">

**Author:** ![MadeByDouglas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/madebydouglas/32/29588_2.png) [@MadeByDouglas](https://forums.swift.org/u/MadeByDouglas)\
**Post date:** [July 4, 2024, 5:00am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/2 "2024-07-04T05:00:44Z")

</div>

This looks great, I am wondering, could Svelte and SvelteKit fit somewhere in this puzzle? Or would that not make sense? I am only a beginner to web technologies, but my understanding is Svelte is rather similar to SwiftUI and since its actually a compiler can do things other javascript frameworks cannot, making it an ideal web technology to use.  
Would it make sense to build a transpiler from SwiftUI to Svelte like what Skip has done to transpile SwiftUI to Kotlin Jetpack Compose? I guess it's a very different approach? [https://skip.tools](https://skip.tools)

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [July 5, 2024, 12:35pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/3 "2024-07-05T12:35:39Z")

</div>

My general approach to software is: anything can be done if you just want it hard enough ; )

But in this case, the goal (at least mine) is to rely on the least amount of javascript as possible.

If you want to use svelte/sveltekit (and there is nothing wrong with that), I think you'll have an easier time just staying in the javascript ecosystem. "transpiling" swift to svelty-javascript feels way to complicated for the possible benefits it might bring.

To me, the more interesting way forward for "normal" web apps is:

- stick to the basics with HTML, CSS, HTTP
- do as much as you can on the server (in a proper language ; )
- if you need client-side state, try to isolate it and keep it simple (htmx, web components, alpine, and similar can help a lot there)
- if you need more sophisticated client-side state, contribute to SwiftWasm and help making it a viable option for fat-client web apps

To the last point, just look at [Tokamak](https://github.com/TokamakUI/Tokamak) or the [Embedded Wasm example](https://github.com/apple/swift-for-wasm-examples) by the wonderful @Max_Desiatov:  
SwiftWasm client apps are definitely possible and basically already here. So my advice/wish would be: let's not spend more time on javascript, but instead give some love (and hard work) to polishing up SwiftWasm frameworks.

I have it on my list to will an embedded-wasm-compatible reactive client-side UI library based on [elementary](https://github.com/sliemeobn/elementary) into existence, but I am a bit short on play time at the moment. If anyone wants to collaborate, please reach out!

---

<div class="post-metadata">

**Author:** ![MadeByDouglas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/madebydouglas/32/29588_2.png) [@MadeByDouglas](https://forums.swift.org/u/MadeByDouglas)\
**Post date:** [July 9, 2024, 8:51am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/4 "2024-07-09T08:51:17Z")

</div>

OK this makes a lot of sense! Thank you, I was hoping for a high level vision for the future to try and understand this better, and thats what you've outlined here.

I think this all sounds great, I will be watching your elementary library for sure.

One question, where would you recommend hosting the swift server component for your simple web app? Like with Svelte, I would just use Vercel or Netlify, what would you recommend? I know Swift.clouds works on fastly via swift wasm, but it seems going native with something like aws lambda would be better, but I'm not sure if aws lambda is really built for serving something like this.

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [July 9, 2024, 7:49pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/5 "2024-07-09T19:49:49Z")

</div>

> [@MadeByDouglas](#):
>
> Thank you, I was hoping for a high level vision for the future to try and understand this better, and thats what you've outlined here.

Not so sure about "vision", more like "random dude on the internet has opinions" ; ) But you are welcome.

> [@MadeByDouglas](#):
>
> One question, where would you recommend hosting the swift server component for your simple web app?

I don't really have an opinion on this, as I mostly deal with on-premises/self-hosted environments. But anywhere should be fine, really. The last [Server Side Meetup](https://www.youtube.com/watch?v=UgON3lsCRwo) had a session about this.

The only general advise I would have is: stay clear of too much vendor-lock-in and stick to the open-source basics: linux, docker containers, postgres/mongo or similar, redis or similar. those fancy fully integrated magic cloud-something-something services might be nice to start, but they lock you in pretty hard.

---

<div class="post-metadata">

**Author:** ![MadeByDouglas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/madebydouglas/32/29588_2.png) [@MadeByDouglas](https://forums.swift.org/u/MadeByDouglas)\
**Post date:** [July 10, 2024, 5:28am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/6 "2024-07-10T05:28:30Z")

</div>

haha, well you are a random dude who knows more than me, so I'm thankful to you and this community for that.

I hear you, I don't yet have those skills so the magic buttons are still appealing to me. That being said, I also have experience with lock in and its perils, so I've come to really like Supabase. I use it for all my server needs, and since it's open source built around postgres, I could spin it off if I needed to. The missing piece has been around front end web deployment and architecture to go along with my mobile apps, so this gives me lots to think about. On another tangent, getting support for Swift within Supabase functions (built on deno deploy) would be really cool, but I understand it's a javascript world.

---

<div class="post-metadata">

**Author:** ![liv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/liv/32/37916_2.png) [@liv](https://forums.swift.org/u/liv)\
**Post date:** [July 11, 2024, 2:22am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/7 "2024-07-11T02:22:42Z")

</div>

> [@sliemeobn](#):
>
> SwiftWasm client apps are definitely possible and basically already here. So my advice/wish would be: let's not spend more time on javascript, but instead give some love (and hard work) to polishing up SwiftWasm frameworks.

I think wasm as a technology still some times away. AFAIK it can't do DOM manipulation.

Currently swift on server focus has been to serve JSON for iOS apps. As it would be easier to share the same language for building App and the API.

I think swift has the capability to compete with MVC web frameworks like Django and Ruby on Rails. I think it makes more sense focus on improvement on web side of features in swift on server over Wasm.

Elementary looks promising as type safe templating language.

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [July 16, 2024, 6:28pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/8 "2024-07-16T18:28:04Z")

</div>

just a quick FYI update:

I tagged a [elementary-htmx 0.2.0](https://github.com/sliemeobn/elementary-htmx) today that includes SSE extensions.

this makes it very easy to live-update parts or your web UI via dead simple SSE streams, like so:

```swift
// in your html
div(.hx.ext(.sse), .sse.connect("/time")) {
    p(.sse.swap("time")) { "Server Time:" }
}

// in your route handler
app.get("time") { _ -> Response in
    Response(
        status: .ok,
        headers: ["Content-Type": "text/event-stream"],
        body: .init(managedAsyncStream: { writer in
            while true {
                try await writer.writeBuffer(.init(string: "event: time\ndata: Server Time: \(Date.now)\n\n"))
                try await Task.sleep(for: .seconds(1))
            }
        })
    )
}

```

the example may not be that impressive, but I am still enamoured by how little it takes with htmx to build robust and efficient(-ish) live-updating web apps - all without touching javascript. it is beautiful.

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [October 3, 2024, 6:12pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/9 "2024-10-03T18:12:18Z")

</div>

thanks to another contribution, [elementary-htmx how supports WebSocktes!](https://github.com/sliemeobn/elementary-htmx/releases/tag/0.3.0)

if you ever wanted to use server-side swift to create a dynamic, live-updating webpage: there has never been a better time ; )

check out the examples for a basic websocket-based "echo" demo:  
[Hummingbird example](https://github.com/sliemeobn/elementary-htmx/tree/main/Examples/HummingbirdDemo)  
[Vapor example](https://github.com/sliemeobn/elementary-htmx/tree/main/Examples/VaporDemo)

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [October 3, 2024, 8:41pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/10 "2024-10-03T20:41:25Z")

</div>

> [@liv](#):
>
> I think wasm as a technology still some times away. AFAIK it can't do DOM manipulation.

Wasm is just an instruction set. It can't and won't do DOM manipulation in the same way that ARM or x86 instruction sets can't do DOM manipulation: it was never in the business of DOM manipulation in the first place.

You'll need libraries for that, the most mature one maintained by the community is [JavaScriptKit](https://github.com/swiftwasm/JavaScriptKit). It allows not only DOM manipulation, but pretty much anything that JavaScript allows. If that's too heavy for your case, [swift-for-wasm-examples](https://github.com/apple/swift-for-wasm-examples) has code samples for drawing on HTML canvas nodes, which can be scaled to any DOM call with macros or code generation plugins.

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [October 3, 2024, 9:15pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/11 "2024-10-03T21:15:42Z")

</div>

on that note, I experimented with a basic "reactive elementary" wasm overlay that manipulates the DOM directly (though JavaScriptKit).

> **[GitHub - sliemeobn/elementary-dom](https://github.com/sliemeobn/elementary-dom)**
>
> Contribute to sliemeobn/elementary-dom development by creating an account on GitHub.

It could be made viable, but for now the wasm binary size makes this basically a non-starter.  
If I have a more playtime, I want to try to get it running in embedded mode - but things are still quite rough for embedded (no variadic generics, no integrated SwiftPM support, a non-established [WIP draft of JavaScriptKit](https://github.com/swiftwasm/JavaScriptKit/pull/251) for embedded, and tons of build flags that are frankly above my paygrade, ...)

I'll report back if I have more notes to compare, but for now, sadly, I think a SwiftWasm based "reactive web frontend library" is just not really what you should bet your future on.

Following up on what @Max_Desiatov describes, I too see it already making a lot more sense as cleanly encapsulated "logic" modules with defined interfaces that you want to use in your browser app (without necessarily driving the entire UI).

PS: I posted a quick video screen capture of it on the [SwiftWasm discord](https://discord.com/channels/780838335798706197/802944960881098752/1289326041747423435).

---

<div class="post-metadata">

**Author:** ![Tyler-Keith-Thompson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tyler-keith-thompson/32/15348_2.png) [@Tyler-Keith-Thompson](https://forums.swift.org/u/Tyler-Keith-Thompson)\
**Post date:** [December 1, 2024, 9:40pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/12 "2024-12-01T21:40:13Z")

</div>

This is fantastic. I've been playing with it a bit and it makes me excited for the future. I really want the Swift WASM ecosystem to evolve to the point where that's my go to solution. Until then, ElementaryHTMX looks really promising!

---

<div class="post-metadata">

**Author:** ![dschaefer2](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dschaefer2/32/46516_2.png) [@dschaefer2](https://forums.swift.org/u/dschaefer2)\
**Post date:** [January 15, 2025, 12:53am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/13 "2025-01-15T00:53:52Z")

</div>

I've started learning about Htmx following ThePrimeagen and friends on YouTube. It was cool to find your ElementaryHTMX doing a web search. Any thoughts of getting it added to the [htmx.org](http://htmx.org) site to make it easier to find? Looks like they love to showcase all the different back ends that can support it. Would be a nice advert for Server-side Swift as well. 🙂

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [January 15, 2025, 7:34am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/14 "2025-01-15T07:34:13Z")

</div>

> [@dschaefer2](#):
>
> Any thoughts of getting it added to the [htmx.org](http://htmx.org) site to make it easier to find?

Good idea, thanks for the hint! I'll see what I can do later today.

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [March 11, 2025, 11:40pm UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/15 "2025-03-11T23:40:18Z")

</div>

It turns out Safari is pretty bad at WS and SSE: [WS extension unreliable on mobile browsers (IOS) · Issue #98 · bigskysoftware/htmx-extensions · GitHub](https://github.com/bigskysoftware/htmx-extensions/issues/98)

Here is my project if anyone wants to check it out: [GitHub - Cyberbeni/index.htmx: Lightweight server-side rendered dashboard.](https://github.com/Cyberbeni/index.htmx)

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [March 12, 2025, 7:28am UTC](https://forums.swift.org/t/swift-htmx-a-new-hope/72755/16 "2025-03-12T07:28:56Z")

</div>

It is great to see some real-life source code with Elementary that is publicly available.

Thanks for sharing!
