How to Run Node Js Code from Npm Inside of Swift

How to run Node JS code from npm inside of Swift

That's a great question. I will try to give you an overview of how these different systems work and a theoretical, but rather painful solution. Swift runs on the mobile end and manages its resources on a more low level approach.

Javascript on the other hand is a general purpose language that requires a Virtual Machine(VM) that "understands" javascript, to be embedded into the system you are targeting. Thankfully, iOS has already done that with javascriptcore interface. With it, you can load up a Javascript bundle and evaluate any function from it.

In theory, what you are suggesting would work like this:

  1. Find a bundled version of the package you want to run

  2. Load it up on the javascript VM using the JavaScriptCore interface

  3. Evaluate the function of the package

Check out this great guide

The great caveat of this approach is the platform specific calls of the npm bundle (for example Node.js' fs package does not exist).

If that is the case then you should use a remote service that runs the code for your as cenk suggested.

Can I run JavaScript inside Swift code?

Last tested with Swift 5.1

Here is an example you can run in Playground to get you started:

import JavaScriptCore

let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"

var context = JSContext()
context?.evaluateScript(jsSource)

let testFunction = context?.objectForKeyedSubscript("testFunct")
let result = testFunction?.call(withArguments: ["the message"])

result would be Test Message: the message.

You also can run JavaScript code within a WKWebView calling evaluate​Java​Script(_:​completion​Handler:​).

You can also run JavaScript within a UIWebView by calling string​By​Evaluating​Java​Script(from:​), but note that that method has been deprecated and is marked as iOS 2.0–12.0.

run nodejs script in xcode run script

instead of node /path/ i had to use the path to the node binary e.g. /usr/local/bin/node "$SRC_DIR/deployment/deploy.js"

Swift encryption and NodeJS decryption producing inconsistent results

Look for cross plate-form AES encryption

Connect a Cloud Function to an iOS app to send mass push notifications at once

How do I connect with Cloud Functions from within an iOS app?

You can call Cloud Functions by doing the same thing that you would do calling any API, you will have to design your Cloud Functions to be Callable Functions, you can find more details on them and how to set it all up, in Swift and other languages, in this Documentation.

No matter what I have to get each follower from the "followers" ref and then I have to get each follower's deviceToken from within their "users" ref, I'm not sure where to start here and how do I actually send a push notification code once inside Cloud Functions? I found this answer but it's in javascript. I don't know javascript but I do know a tad bit of Node.js

These 2 questions can be answered together by this community answer that states that you should integrate Firebase Cloud Messaging into your iOS app, with a link to the full documentation on that subject. Also, you can find in this Documentation how you can send notifications to multiple devices, in there you will also find an example of the code that you will need to use in the Cloud Function itself using the Admin SDK.

NOTE: Cloud Functions can be written in Node.js, Go, Java and Python, and all the sample codes for the Cloud functions are in those languages.



Related Topics



Leave a reply



Submit