Just a second...

Start publishing with Swift on iOS®

Create a client that publishes data through topics on the Diffusion™ server. This example uses Swift to create a publishing client.

To complete this example, you need Apple's Xcode installed on your development system and a Diffusion server.

You also require a named user that has a role with the modify_topic and update_topic permissions. For example, the "TOPIC_CONTROL" role. For more information about roles and permissions, see Role-based authorization.

This simple publishing example connects to the Diffusion server using a principal and password credentials. It then adds a string topic. Once it has received a response from Diffusion to say that the topic has been created it then updates it to a value of "Hello World" using the non-exclusive string value updater, obtained from the topic update control feature.

You will need a caller to instantiate a new SimplePublisher instance and then call publish().

Full example

The completed publishing client class contains the following code:
import Foundation
import Diffusion


class SimplePublisher {
    func publish() {
        // Connect to Diffusion Cloud
        let credentials = PTDiffusionCredentials(password: "<password>")
        let configuration = PTDiffusionSessionConfiguration(
            principal: "<system user>",
            credentials: credentials)
        let url = NSURL(string: "wss://host_name")

        PTDiffusionSession.open(with: url! as URL, configuration: configuration) {
        (session, error) -> Void in
            // Add the topic
            session!.topicControl.add(withTopicPath: "example/topic",
            type: PTDiffusionTopicType.string) {
            (error) -> Void in
                // Update the topic
                let updater = session!.topicUpdateControl.updater.stringValueUpdater()
                updater.update(withTopicPath: "example/topic", value: "Hello World") {
                (error) -> Void in }
            }
        }
    }
}