Using Node.js with Magic XPA

Monday, 10th April, 2017

Node.js - you either know it and you love it, or you don't know it. Basically, it's the universal solvent for application development. Nothing you can't do. Let's take some small, small baby-steps integrating the power of Node.js with (the power of) Magic XPA. It won't be the last time I touch on either in this blog.

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

See more here: https://nodejs.org/en/

Using Node.js with Magic XPA

You write cool stuff with Node.js, it's javascript, it runs on the server, it runs on the client, and just gets things done. Now you can integrate it directly with Magic XPA. Maybe you want Magic to work with MongoDB, or Postgres, maybe you want Single-User edition to work with MSSQL or MySql, maybe you want to work with Arduino, with bluetooth, launch a web server or a Rest API or work with web sockets, talk to Alexa, switch off your lights, cue your playlist, all good. It's all available by proxy in Magic. Let's go.

  • Demo

  • This is the demo, ugly as hell. Run program "Hello, Node.js" in the Demo project. It's a simple request/response from Magic to Node.js and return. Not sexy, just the basic mechanic of the integration. You tell it your name, node.js launches, processes it and say "Hello" right back. This is all marshalled by a Magic c# snippet.

    Program "Run External JS Script" demonstrates loading and launching an external script. It simply adds two numbers you pass from Magic but it could be anything. Again, this is all orchestrated by a Magic c# snippet.

    The source code of the demo is located here: https://github.com/craigcode/xpa-nodejs

    Let's walk through what's going on.

  • 1. Install and Set-Up
  • Ok, so it's beyond the scope of this article for me to teach node.js from scratch or its install ( https://nodejs.org/en/ - check node works with "node -v" on the command-line afterwards) but I will reference a package Edge.js that is crucial to our integration. It's available here: http://tjanczuk.github.io/edge/#/ - you can use Nuget https://dist.nuget.org ("> nuget install edge.js") to get the package for node.js however I also included it in the resources directory on github. You need to copy both EdgeJS.dll and the edge directory into the Magic XPA home directory.

  • 2. Composite Resource Repository

  • Next we define a reference to EdgeJS.dll as a .Net resource. System.Windows.Forms is not mandatory, I used it during testing.

  • 3. Program Repository

  • Get JSDir for Require (p) evaluates and returns the location of the JS directory beneath the project folder using the %WorkingDir% logical name. This contains an example of an external script, addnumbers.js, used in the second demo program, "Run External JS Script".

    "Hello, Node.js" and "Run External JS Script" are the demo programs you may execute mentioned previously.

  • 4. Hello, Node.js

  • This is a very simple program, control is marshalled in the C# snippet which utilises Edge.js as conduit to Node.js. There are a couple of different ways to work with Node.js here, but this example uses a Task.Wait() call to contain and better manage for Magic the asynchronous callback flow of Node.js.

     

  • 5. Run External JS Script C# Snippet

  • Again, a very simple program that calls Node.js to add 2 numbers managed by a C# snippet. For illustrative purposes, I load the script from the project JS directory.

     

    That's all. It's just code, how difficult can it be?

  • Why?
  • Why not?

  • Wouldn't it be easier to just publish what you need in Node.js as a Rest API and consume it on demand from Magic?
  • Perhaps.

  • Next steps
    • 1. Look up.
    • 2. Learn Node.js if you don't already know it.
    • 3. Find the joy.

You're welcome.

Hokey religions and ancient weapons are no match for a good blaster at your side, kid.
Han Solo