Settings

Theme

NodeSwift: Bridge Node.js and Swift

github.com

103 points by joshuawright11 2 years ago · 25 comments

Reader

guzik 2 years ago

Oh my, yes! We have some legacy microservices written in Swift/Vapor (while our main stack is NestJS) and instead of rewriting them to TypeScript, we can use this. Cool!

  • joshuawright11OP 2 years ago

    What drove the switch from Vapor -> Nest?

    • guzik 2 years ago

      Actually, we are using Node/Nest from the start. A significant amount of our business logic was written in Swift for our iOS mobile app, which we eventually moved to the microservices (Vapor). This was because we needed to create an Android app and didn't want to write new code from scratch.

anonymouse008 2 years ago

Wait, so you compile a swift executable that’s callable from js? How would one distribute this client side in browser?

I’m not smart enough to understand this. I love swift, vapor and node individually, I don’t get it together

  • wk_end 2 years ago

    Node’s a server-side thing, so I don’t think you’d distribute these in the browser.

    • vips7L 2 years ago

      It's a desktop thing too when working with cli tools or electron, though I've found that for electron I just end up calling other languages via IPC on stdin/out. It might not be as fast as ffi from node, but its certainly leagues easier and clearer.

  • demondemidi 2 years ago

    Other way round.

    NodeJS has had FFI for over a decade that allows you to call functions in .dylib/.a/.dll on Mac/Linux/Windows. I've used it extensively to link with libraries provided by USB device manufacturers to avoid direct USB interfacing.

    See:

    https://www.npmjs.com/package/ffi

    This let's you call Node from inside Swift.

  • robmccoll 2 years ago

    You wouldn't run this in the browser. On the frontend, this would be useful for Electron apps (which run an embedded Chromium for the UI and a local node server for business logic and interfacing with the operating system) on MacOS. On the backend, this might be useful if you have a node-based API and want to share some Swift code between your iOS frontend and node backend.

  • brailsafe 2 years ago

    It might be possible to compile it to web assembly and then call it in the browser.

koinedad 2 years ago

This is cool, don’t know how I’d use it yet but having access to the huge library of node modules sounds like a win.

floydnoel 2 years ago

this is freakin great

elpakal 2 years ago

I get paid to write Swift code at the day job, at night I write a lot of node backend code for a side project. I have to say that it's pretty refreshing to jump in and not have the (increasingly) opinionated Swift compiler barking at me for everything I do. Unfortunately Swift has also made me extremely paranoid in JS, where I find that everything I write is checked for null, but I don't think I'd trade the freedom in nodeJS for Swift's type safety. This is super cool though, well done.

  • s4i 2 years ago

    > I don't think I'd trade the freedom in nodeJS for Swift's type safety

    Surely you have considered TypeScript for your Node project then?

quotemstr 2 years ago

We've got to get out of the business of these pairwise language interop solutions.

goestoo 2 years ago

So, the main use case of this is for writing Electron apps, the biggest benefit of Electron is having cross-platform apps, using swift code in your Electron app that's only work on macOS gonna defeat of propose of an Electron app.

  • mmcclure 2 years ago

    This has already been corrected, but just calling out a bullet from the feature section:

    > Cross-platform: NodeSwift works not only on macOS, but also on Linux, Windows, and even iOS!

    Now I'm curious about the iOS bit. Is that intended for React Native?

  • dieortin 2 years ago

    Swift code can run on Windows and Linux too

    • goestoo 2 years ago

      Sorry, I didn't know that, I thought swift is only for Apple platforms.

      • pdpi 2 years ago

        Nope! It's entirely unremarkable on Linux (in a good way: you wouldn't notice that it's not its "native" platform), and it's surprisingly pleasant to use (and probably the easiest way to get a good C compiler) on Windows.

  • adamnemecek 2 years ago

    Electron apps can have native components and in fact many do.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection