Example: Make non-exclusive updates to a topic
The following examples use the Diffusion™ API to update a topic with content. Updating a topic this way does not prevent other clients from updating the topic.
JavaScript
// 1. A session may update any existing topic. Update values must be of the same type as the topic being updated. // Add a topic first with a string type session.topics.add('foo', '').then(function() { // Update the topic return session.topics.update('foo', 'hello'); }).then(function() { // Update the topic again return session.topics.update('foo', 'world'); }); // 2. If using RecordContent metadata, update values are constructed from the metadata // Create a new metadata instance var meta = new diffusion.metadata.RecordContent(); meta.addRecord('record', 1, { 'field' : meta.integer() }); // Create a builder to set values var builder = meta.builder(); builder.add('record', { field : 123 }); // Update the topic with the new value session.topics.add('topic', '').then(function() { session.topics.update('topic', builder.build()); });
Apple
@import Diffusion; @implementation TopicUpdateExample { PTDiffusionSession* _session; } -(void)startWithURL:(NSURL*)url { PTDiffusionCredentials *const credentials = [[PTDiffusionCredentials alloc] initWithPassword:@"password"]; PTDiffusionSessionConfiguration *const sessionConfiguration = [[PTDiffusionSessionConfiguration alloc] initWithPrincipal:@"control" credentials:credentials]; NSLog(@"Connecting..."); [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."); // Set ivar to maintain a strong reference to the session. _session = session; // Add topic. [self addTopicForSession:session]; }]; } static NSString *const _TopicPath = @"Example/Updating"; -(void)addTopicForSession:(PTDiffusionSession *const)session { // Add a single value topic without an initial value. [session.topicControl addWithTopicPath:_TopicPath type:PTDiffusionTopicType_SingleValue value:nil completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Failed to add topic. Error: %@", error); } else { NSLog(@"Topic created."); // Update topic after a short wait. [self updateTopicForSession:session withValue:1]; } }]; } -(void)updateTopicForSession:(PTDiffusionSession *const)session withValue:(const NSUInteger)value { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ { // Get the non-exclusive updater. PTDiffusionTopicUpdater *const updater = session.topicUpdateControl.updater; // Prepare data to update topic with. NSString *const string = [NSString stringWithFormat:@"Update #%lu", (unsigned long)value]; NSData *const data = [string dataUsingEncoding:NSUTF8StringEncoding]; PTDiffusionContent *const content = [[PTDiffusionContent alloc] initWithData:data]; // Update the topic. [updater updateWithTopicPath:_TopicPath value:content completionHandler:^(NSError *const error) { if (error) { NSLog(@"Failed to update topic. Error: %@", error); } else { NSLog(@"Topic updated to \"%@\"", string); // Update topic after a short wait. [self updateTopicForSession:session withValue:value + 1]; } }]; }); } @end
Java
and Android
import com.pushtechnology.diffusion.client.Diffusion; import com.pushtechnology.diffusion.client.callbacks.TopicTreeHandler; import com.pushtechnology.diffusion.client.features.control.topics.TopicControl; import com.pushtechnology.diffusion.client.features.control.topics.TopicControl.AddCallback; import com.pushtechnology.diffusion.client.features.control.topics.TopicUpdateControl; import com.pushtechnology.diffusion.client.features.control.topics.TopicUpdateControl.Updater.UpdateCallback; import com.pushtechnology.diffusion.client.session.Session; import com.pushtechnology.diffusion.client.topics.details.TopicType; /** * An example of using a control client to create and update a topic in non * exclusive mode (as opposed to acting as an exclusive update source). In this * mode other clients could update the same topic (on a last update wins basis). * <P> * This uses the 'TopicControl' feature to create a topic and the * 'TopicUpdateControl' feature to send updates to it. * <P> * To send updates to a topic, the client session requires the 'update_topic' * permission for that branch of the topic tree. * * @author Push Technology Limited * @since 5.3 */ public final class ControlClientUpdatingSingleValueTopic { private static final String TOPIC = "MyTopic"; private final Session session; private final TopicControl topicControl; private final TopicUpdateControl updateControl; /** * Constructor. */ public ControlClientUpdatingSingleValueTopic() { session = Diffusion.sessions().principal("control").password("password") .open("ws://diffusion.example.com:80"); topicControl = session.feature(TopicControl.class); updateControl = session.feature(TopicUpdateControl.class); // Create the topic and request that it is removed when the session // closes topicControl.addTopic( TOPIC, TopicType.SINGLE_VALUE, new AddCallback.Default() { @Override public void onTopicAdded(String topicPath) { topicControl.removeTopicsWithSession( TOPIC, new TopicTreeHandler.Default()); } }); } /** * Update the topic with a string value. * * @param value the update value * @param callback the update callback */ public void update(String value, UpdateCallback callback) { updateControl.updater().update(TOPIC, value, callback); } /** * Close the session. */ public void close() { session.close(); } }
.NET
Change the URL from that provided in the example to the URL of Diffusion Cloud. Diffusion Cloud service URLs end in diffusion.cloud
This page last modified: 2015/04/17