We use cookies for website functionality that cannot be switched off in our systems. You can set your browser to block cookies, but some parts of the site may not work properly. These cookies do not store any personally identifiable information. See this page for further information. Hide this message.

Get smart things talking with AllJoyn and Kii

As members of the AllSeen Alliance we’re excited about the AllJoyn™ framework for Internet of Things (IoT) projects. The de facto object-oriented approach, AllJoyn reduces development complexity by allowing for proximity peer to peer over various transports. It’s written in C++ at its core, provides multiple language bindings (such as Java, JavaScript, C#, ObjC) and supports complete implementations across various operating systems and chipsets.

Below are the steps to quickly get started with AllJoyn using JavaScript binding (in nodejs) to allow peers to exchange messages. You’ll also learn how to cloud-enable the project by logging received messages on Kii for later processing. In this tutorial we’re sending text messages but Kii can richer data such as music, photo, video, etc so you can be sending songs to smart speakers or photos to a smart TV with cloud-enabled storage and Analytics, for example.

AllJoyn framework overview

Before jumping into code, you should know a few AllJoyn basics. Any peer participating in an AllJoyn conversation must “talk” via a (relatively low level) bus. Think of it as the highway in which all AllJoyn messages travel. First your app needs to attach to the bus by creating a bus attachment. Through a bus a peer can participate in an AllJoyn conversation but it has to be done through a concrete interface. The interface is created in the bus with the purpose of handling shared objects and signals sent to the objects. In our example the peers will share a chat object in the bus and signals sent to it will be relayed to the other peers as messages.

Next you have to decide whether your app wants to join and use an existing service or advertise one for others to join. Assuming you want to advertise a service, give it a recognizable name. Other peers will try to locate your service using it. Then bind a port (e.g. 27) to create a session over the bus. The steps:

Create a BusAttachment and connect to the AllJoyn framework to communicate with other AllJoyn applications

Create an Interface in the Bus and define the Signals that it will handle

Create a BusObject and pair it with the Interface (after starting the Bus)

Define a SignalHandler for signals sent to the BusObject

Register the BusObject with the Bus and connect

If Advertising:

Choose a unique identifier. Create a unique name

Create a Session other applications can join

Tell other applications you’re there by advertising the service name

If Joining:

Find other applications nearby by looking for a name prefix

Define a BusListener to join a Session once a service name is found

Join a session that has been found

Communicate with other applications in your group

Don’t panic. We’ll go through the code needed to achieve all the necessary steps below.

Install and compile AllJoyn

Before taking AllJoyn for a spin let’s install it. You could download the source and compile it yourself (e.g. for a Raspberry Pi) but the easiest way is to use the npm package (thanks Octoblu!) and test the framework with the JavaScript binding (requires nodejs). Installation goes like this:

Download the Kii js SDK and place on the same directory where you’ll run your scripts

Test the example-host.js and example-client.js by Octublu to make sure everything works

On Mac the scripts are located on /opt/local/lib/node_modules/alljoyn (if you installed globally with -g) and can be run with the node command

Advertise the chat service

Next take a look at our nodejs script. It can run in host mode to advertise a chat service for other peers to join. Or you can run it as a client, which will look for a service advertised by the host on the network by name, and then connect to it. This setup allows as many clients as you want but there must be only one host. The reason for using a single script for host and client is that 95% of the code is shared (see below).

Once connected all peers (host and clients) have the same behavior listening to and showing incoming messages, and allowing you to type messages for other peers to see. As an add-on all hosts and clients log incoming messages on the cloud with Kii for later analysis.

Initialize and authenticate with Kii

Once you created a Kii app at developer.kii.com, write down the app ID and key to initialize Kii:

Please note: In order to store Kii data you’ll need to create a KiiObject from an application scope bucket and set all the details of the incoming message as in a key/value store. After that you can save the object on Kii with an async call and use the Kii Data Browser available on developer.kii.com to see stored messages:

That’s about it. Keep in mind that peers interpret messages as text to display on screen but these exchanged messages could be more complex structures, commands, etc. suitable for all kinds of IoT scenarios. Even binary formats could be exchanged and saved on Kii as files.And of course, besides storing data on Kii Cloud you can retrieve it via a powerful query system as easily.

Wrap up

Getting started with AllJoyn is easy thanks to the npm package with js binding. Since Kii provides JavaScript SDK integration with the binding, you can have an AllJoyn enabled chat service with cloud logging up and running in no time. Keep in mind that peers could easily be IoT devices interpreting the messages as commands to do something meaningful or exchanging richer data such as streaming music (more on that coming to the Kii Blog soon).

Full source code for both the host and client can be found here. Usage is straightforward:

MODE is either “host” or “client”. Run in host mode first, then open another shell and run the client mode (if you get errors about the address being in use try from another machine on the network since the embedded AllJoyn daemon is then using the same address). Note that you should run only one host but can have as many clients as you want. The parameters YOUR_APP_ID and YOUR_APP_KEY can be obtained by creating an app at developer.kii.com. And you can freely set your username and password, which will be created the first time if credentials don’t exist on Kii. We recommend using a different username for each running script (otherwise all scripts no matter if they are hosts or clients will do the logging as the same Kii user).

Happy coding 🙂

Share:

One thought on “Get smart things talking with AllJoyn and Kii”

I am interested in trying the Alljoyn JavaScript interface. However, when trying to install the Alljoyn npm package in the command prompt on my Windows computer I get the following error message:
‘ls’ is not recognized as an internal or external command, operable program or batch file.
ls is a Unix command and I have tried to find a solution but failed.
Any idea?

The Gartner Cool Vendor Logo is a trademark and service mark of Gartner, Inc., and/or its affiliates, and is used herein with permission. All rights reserved. Gartner does not endorse any vendor, product, or service depicted in its research publications, and does not advise technology users to select only those vendors with the highest ratings or other designation. Gartner research publications consist of the opinions of Gartner's research organization and should not be construed as statements of fact. Gartner disclaims all warranties, expressed or implied, with respect to this research, including any warranties of merchantability or fitness for a particular purpose.

The Gartner Cool Vendor Logo is a trademark and service mark of Gartner, Inc., and/or its affiliates, and is used herein with permission. All rights reserved. Gartner does not endorse any vendor, product, or service depicted in its research publications, and does not advise technology users to select only those vendors with the highest ratings or other designation. Gartner research publications consist of the opinions of Gartner's research organization and should not be construed as statements of fact. Gartner disclaims all warranties, expressed or implied, with respect to this research, including any warranties of merchantability or fitness for a particular purpose.

Learn More

Find out if Kii is right for your initiatives with a custom demo. We’re happy to take you on a tour of:

Kii Platform

Specific features

Kii global distribution services

Implementation processes

Error submitting:

Your information has been submitted! Our team will follow up shortly.

Sign Up - it’s free - and start creating apps on Kii.

By clicking SIGN UP, you agree to our Terms. No credit card required.Please check our GDPR page if you want to obtain personal data of EU residents through applications or IoT devices developed using Kii Cloud.

Kii TO CHINA

Find out if Kii is right for your initiatives with a custom demo. We’re happy to take you on a tour of: