Creating a Topic

This page explains how to create a topic for each scope.

Creating an application-scope topic

Only an application admin can create an application-scope topic. All authenticated users can subscribe the topic, but only the topic creator (i.e. the application admin) can send push messages to the topic by default.

Here is the sample code for creating an application scope topic.

  • // Authenticate as the app administrator.
    // Check the ClientID and ClientSecret in the Kii Cloud developer portal.
    Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", {
      success: function(adminContext) {
        // Create a topic in the application scope.
        var topicName = "SendingAlert";
        var topic = adminContext.topicWithName(topicName);
    
        // Save the topic to Kii Cloud.
        topic.save({
          success: function(theTopic) {
            // Do something.
          },
          failure: function(errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(errorString, errorCode) {
        // Handle the error.
      }
    });
  • // Authenticate as the app administrator.
    // Check the ClientID and ClientSecret in the Kii Cloud developer portal.
    Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then(
      function(adminContext) {
        // Create a topic in the application scope.
        var topicName = "SendingAlert";
        var topic = adminContext.topicWithName(topicName);
    
        // Save the topic to Kii Cloud.
        return topic.save();
      }
    ).then(
      function(theTopic) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
    
        // Get the topic for the failed topicWithName() method.
        var theTopic = error.target;
        // Get the error message.
        var errorString = error.message;
      }
    );

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").

Creating a group-scope topic

Any group members can create a group-scope topic. All group members can subscribe to the topic, and all of them can send push messages to the topic by default.

Unlike an app-scope topic, a group-scope topic is usually created dynamically by an application. See the following sample code to see how to do this:

  • // Create a group.
    var groupName = "myNewGroup";
    var group = Kii.groupWithName(groupName);
    group.save({
      success: function(theGroup) {
        // Create a topic in the group scope.
        var topicName = "GroupTopic";
        var topic = theGroup.topicWithName(topicName);
    
        // Save the topic to Kii Cloud.
        topic.save({
          success: function(theTopic) {
            // Do something.
          },
          failure: function(errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theGroup, errorString) {
        // Handle the error.
      }
    });
  • // Create a group.
    var groupName = "myNewGroup";
    var group = Kii.groupWithName(groupName);
    group.save().then(
      function(theGroup) {
        // Create a topic in the group scope.
        var topicName = "GroupTopic";
        var topic = theGroup.topicWithName(topicName);
    
        // Save the topic to Kii Cloud.
        return topic.save();
      }
    ).then(
      function(theTopic) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
    
        // Get the group for the failed save() method.
        var theGroup = error.target;
        // Get the topic for the failed topicWithName() method.
        var theTopic = error.target;
        // Get the error message.
        var errorString = error.message;
      }
    );

Here's a brief description of the sample code:

  • Creates a group-scope topic by calling the topicWithName method.
  • Saves the topic by calling the save method. You need to call the save method in order to reflect the change on Kii Cloud.

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").

Creating a user-scope topic

Any authenticated users can create a user-scope topic. Only this user can subscribe to the topic and send push messages to it by default.

Like a group-scope topic, a user-scope topic is usually created dynamically by an application. See the following sample code to learn how to create a user-scope topic.

  • // Create a topic in the user scope.
    var topicName = "MyTODO";
    var user = KiiUser.getCurrentUser();
    var topic = user.topicWithName(topicName);
    
    // Save the topic to Kii Cloud.
    topic.save({
      success: function(theTopic) {
        // Do something.
      },
      failure: function(errorString) {
        // Handle the error.
      }
    });
  • // Create a topic in the user scope.
    var topicName = "MyTODO";
    var user = KiiUser.getCurrentUser();
    var topic = user.topicWithName(topicName);
    
    // Save the topic to Kii Cloud.
    topic.save().then(
      function(theTopic) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
    
        // Get the topic for the failed topicWithName() method.
        var theTopic = error.target;
        // Get the error message.
        var errorString = error.message;
      }
    );

Here's a brief description of the sample code:

  • Creates a user-scope topic by calling the topicWithName method.
  • Saves the topic by calling the save method. You need to call the save method in order to reflect the change on Kii Cloud.

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").