Skip to content

Linking Servers

wwitman edited this page Oct 23, 2015 · 1 revision

Linking Zetta servers

Zetta servers allow for establishing a secure tunneled link between two servers. This connection takes care of network configurations, and firewalls that make Cloud connected IoT solutions difficult to maintain.

Zetta then exposes an API at the Cloud endpoint for developers to consume.

Zetta Deployment

In this topic, we'll show you how to link a locally running Zetta server to a server instance deployed in the Cloud. Zetta then exposes an API at the Cloud endpoint that developers can consume. It's very cool and very simple to do!

Deploy Zetta to Heroku

To make things easy, we'll deploy an existing Zetta server that resides on GitHub to the Heroku platform. The README for this GitHub repo includes the Deploy to Heroku button, which does all the work for you.

Note: To do these steps, you'll need to have an Heroku account.

  1. In a browser, go to the Zetta Cloud Starter repository on GitHub.

  2. Click the Deploy to Heroku button in the README.

  3. You'll be asked to log in to your Heroku account, or register for a free account.

  4. In the deployment form, enter an app name (if you don't, the system will pick a random one for you).

  5. Click Deploy for Free.

  6. In a browser (or in a REST client like Postman), hit this URL:

    https://<your-heroku-app-name>.herokuapp.com

You'll get back a standard Zetta server response, like this:

{
    "class": [
        "root"
    ],
    "links": [
        {
            "rel": [
                "self"
            ],
            "href": "https://zetta-test-1234.herokuapp.com/"
        },
        {
            "title": "cloud",
            "rel": [
                "http://rels.zettajs.io/server"
            ],
            "href": "https://zetta-test-1234.herokuapp.com/servers/cloud"
        },
        {
            "rel": [
                "http://rels.zettajs.io/peer-management"
            ],
            "href": "https://zetta-test-1234.herokuapp.com/peer-management"
        }
    ],
    "actions": [
        {
            "name": "query-devices",
            "method": "GET",
            "href": "https://zetta-test-1234.herokuapp.com/",
            "type": "application/x-www-form-urlencoded",
            "fields": [
                {
                    "name": "server",
                    "type": "text"
                },
                {
                    "name": "ql",
                    "type": "text"
                }
            ]
        }
    ]
}

Linking to the Cloud server

Here is where things get interesting. You can link another Zetta server to this Cloud instance. For example, if you have a locally running Zetta hub, and link it to a Cloud server, you can access the local hub from anywhere!

In the rest of this example, we'll use Links to control the Mock LED project from the Zetta instance running on Heroku.

  1. The first thing you'll need is a locally running Zetta hub. For simplicity, we recommend that you implement and use the Mock Device tutorial project, get it running, and make sure it's working correctly.

  2. cd to the root directory of the project, which we called hello-zetta.

  3. In an editor, open the index.js file.

  4. Add the link() function to the hub server, like this, where zetta-test-1234 is the name of the Heroku app -- yours will be different.

    link('https://zetta-test-1234.herokuapp.com/')

    The server code should look something like this:

    var zetta = require('zetta');
    var LED = require('zetta-led-mock-driver');
    
      zetta()
        .name('Device Tutorial')
        .use(LED)
        .link('https://zetta-test-1234.herokuapp.com/')
        .listen(1337, function(){
           console.log('Zetta is running at http://127.0.0.1:1337');
      });
  5. Start the local Zetta server: node index.js. Notice in the server output that the peer connection was established:

    Oct-23-2015 15:44:27 [scout] Device (led) f826f6e0-ebb8-430a-9e1e-6efddebc42fc was provisioned from registry.
    Oct-23-2015 15:44:27 [server] Server (Device Tutorial) Device Tutorial listening on http://127.0.0.1:1337
    Zetta is running at http://127.0.0.1:1337
    Oct-23-2015 15:44:28 [peer-client] WebSocket to peer established (wss://zetta-test-1234.herokuapp.com/peers/Device Tutorial)
    Oct-23-2015 15:44:28 [peer-client] Peer connection established (wss://zetta-test-1234.herokuapp.com/peers/Device Tutorial)
    
  6. Now, point the Zetta browser client app to the server running on Heroku. In your browser, simply enter this URL, and substitute the name of your Heroku app for zetta-test-1234.

    http://browser.zettajs.io/#/overview?url=https://zetta-test-1234.herokuapp.com

  7. Toggle the turn-on/turn-off button.

    Zetta Browser with LED

  8. Notice that the browser shows the state transition very nicely. And, if you look at your local server's stdout, you'll see output indicating each state change:

    Oct-23-2015 15:49:36 [device] led transition turn-on
    Oct-23-2015 15:49:42 [device] led transition turn-off
    

Congratulations! You can now control the mock LED running on your local machine from anywhere in the world!

Clone this wiki locally