Start publishing with OS X/macOS
Create an OS X®/macOS® client that publishes data through topics on the Diffusion™ server.
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 "TOPIC_CONTROL" role. For more information about roles and permissions, see Role-based authorization.
and permissions. For example, theThe client publishes a value to the foo/counter topic every second. You can subscribe to the foo/counter topic by creating a client to subscribe to the topic. For more information, see Start subscribing with iOS.
Full example
The completed implementation of the publishing client files contain the following code:
main.m:
@import Foundation; #import "CounterPublisher.h" /** Wrapper around the counter publisher example class demonstrating how it can be launched as a command line tool. */ int main(int argc, const char * argv[]) { @autoreleasepool { CounterPublisher *const publisher = [CounterPublisher new]; NSURL *const url = [NSURL URLWithString:@"wss://hostname"]; [publisher startWithURL:url]; // Run, Infinite Loop. [[NSRunLoop currentRunLoop] run]; } return 0; }
CounterPublisher.h:
@import Foundation; @interface CounterPublisher : NSObject -(void)startWithURL:(NSURL *)url; @end
CounterPublisher.m:
@import Diffusion; @interface CounterPublisher () @property(nonatomic) PTDiffusionSession* session; @end @implementation CounterPublisher { NSUInteger _counter; } @synthesize session = _session; static NSString *const _TopicPath = @"foo/counter"; -(void)startWithURL:(NSURL *)url { NSLog(@"Connecting..."); // Connect with control client credentials. PTDiffusionCredentials *credentials = [[PTDiffusionCredentials alloc] initWithPassword:@"password"]; PTDiffusionSessionConfiguration *sessionConfiguration = [[PTDiffusionSessionConfiguration alloc] initWithPrincipal:@"principal" credentials:credentials]; [PTDiffusionSession openWithURL:url configuration:sessionConfiguration completionHandler:^(PTDiffusionSession *session, NSError *error) { if (!session) { NSLog(@"Failed to open session: %@", error); return; } // At this point we now have a connected session. NSLog(@"Connected."); // Maintain strong reference to session instance. self.session = session; // Send request to add topic for publishing to. [session.topicControl addWithTopicPath:_TopicPath type:PTDiffusionTopicType_SingleValue value:nil completionHandler:^(NSError *error) { if (error) { NSLog(@"Failed to add topic: %@", error); return; } // At this point we now have a topic. [self updateCounter]; }]; }]; } -(void)updateCounter { // Get the updater to be used for non-exclusive topic updates. PTDiffusionTopicUpdater *updater = self.session.topicUpdateControl.updater; // Format string content for the update. NSString *string = [NSString stringWithFormat:@"%lu", (unsigned long)_counter++]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; PTDiffusionContent *content = [[PTDiffusionContent alloc] initWithData:data]; // Send request to update topic. NSLog(@"Updating: %@", string); [updater updateWithTopicPath:_TopicPath value:content completionHandler:^(NSError *error) { if (error) { NSLog(@"Failed to update topic: %@", error); } }]; // Schedule another update in one second's time. __weak CounterPublisher *const weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ { [weakSelf updateCounter]; }); } @end
This page last modified: 2015/05/01