Defining an Owner

To operate a thing from a mobile application, you need to specify a Kii Cloud user as the thing owner. See here for the overview.

You can create a user with the User Management Feature of the Kii Cloud SDK. After you create a user, you will pass the user ID and access token of this user to the initialization API.

See Logging in and Using an Access Token to learn more about the access token.

There are several ways for creating a user. This guide uses the pseudo user which is also used in the development guides for Android and iOS.

By using the pseudo user, you will create a user without any name and keep using their access token thereafter. Since it does not require any username or password, you can leverage the user without any explicit login.

This method is the easiest if your scenario is simple (e.g., just sending commands and browsing state). However, especially when you use a web browser, be careful that you will not be able to access the pseudo user if you lose the saved access token.

Should you want to secure a login method after the access token is cleared or develop more complex apps by leveraging the Kii Cloud SDK features, you can create a user or group and set them as the thing owner. See Implementation tips at the bottom of this page for more discussion.

Creating a pseudo user

The following sample code shows an example of creating a pseudo user. In this example, a new pseudo user is created and their user ID and access token are stored in the device storage (please note that the implementation of the data storing method is omitted in the sample code).

The access token and user ID are used when initializing the Thing-IF SDK. If you lose the access token, you will no longer be able to access to this user because there will be no way to distinguish the user.

  • // Set predefined and custom fields.
    const userFields = { };
    ...
    
    // Register a pseudo user.
    kii.KiiUser.registerAsPseudoUser(null, userFields).then((authedUser)=> {
      // Get the access token of the preudo user.
      const accessToken: string = authedUser.getAccessToken();
    
      // Get the user ID.
      const userID: string = authedUser.getID();
    
      // Store the access token and the user ID with your own function.
      storeUser(accessToken, userID)
    }).catch((error) => {
      // Handle the error.
      const theUser = error.target;
      const errorString = error.message;
    });
  • // Set predefined and custom fields.
    var userFields = { };
    ...
    
    // Register a pseudo user.
    KiiUser.registerAsPseudoUser(null, userFields).then(
      function(authedUser) {
        // Get the access token of the preudo user.
        var accessToken = authedUser.getAccessToken();
    
        // Get the user ID.
        var userID = authedUser.getID();
    
        // Store the access token and the user ID with your own function.
        storeUser(accessToken, userID)
      }
    ).catch(
      function(error) {
        // Handle the error.
        var theUser = error.target;
        var errorString = error.message;
      }
    );

The basic steps are as follows.

  1. Execute the registerAsPseudoUser method to create and register a pseudo user.
  2. After the user creation and login is executed, execute the getAccessToken and getID methods to get the access token and the user ID of this pseudo user, respectively. Then, save them in the device.

Next, execute the following code to specify the user who will be the thing owner. Finally, pass owner and accessToken obtained in the above code to the constructor of ThingIFAPI when you initialize the Thing-IF SDK.

  • // Create a typed ID from an ID type and the user ID.
    const owner = new ThingIF.TypedID(ThingIF.Types.User, userID);
  • // Create a typed ID from an ID type and the user ID.
    var owner = new ThingIF.TypedID(ThingIF.Types.User, userID);

This code creates a TypedID with the user ID and Types.User that specifies the type of the ID. If you are adding a group as an owner, pass the group ID with Types.Group.

By using this "pseudo user as a thing owner" implementation on multiple devices, you will be able to share a thing among users who own the devices. This is done by binding the thing to the multiple users (i.e., pseudo users). This approach is OK as the Thing-IF SDK supports sharing a thing by multiple owners.

If you are planning to expand your application with the Kii Cloud SDK features (e.g., use the data management to store extra data), this simple relationship between the thing and users might not be sufficient (e.g., the user scope data are created per device, and they cannot be shared among users). Please refer to the next section "Implementation tips".

If you simply insert the above codes in the initialization process, a new user will be created every time the application is launched. Please implement the appropriate logic to reuse the access token and user ID stored with storeUser when the application is launched for the second time.

Implementation tips

User type of the owner

We've presented a method to add a pseudo user as the thing owner. Alternatively, you can use the following approaches. All of them leverage the Kii Cloud SDK features.

  • Create a user with the username (or email address/phone number) and password, and let the user logs in with them (ref. Managing Users)
  • Create a group and set the group as the thing owner to share the thing among multiple group members (ref. Managing Groups)

Getting a command result

To get a command result, you need to receive its command ID as a push notification. However, such a push notification is sent only to the devices associated with the command sender. Design your mobile app with this point in mind.

If the owner is a pseudo user

The command ID is notified only to the device on which the command is executed because a pseudo user is unique to each device. You do not receive notifications for commands issued on other devices which share the thing.

If the owner is a normal user

The command ID is notified to all the devices associated with the owner by installing devices.