Conditional Pub/Sub¶
The example demonstrates publishing a weather forecast message with headers which can be used to create a conditional subscriber. The subscriber uses a selector to get weather forecast messages for the Dallas
area when the expected temperature is higher than 60F
degrees. The message is published into the demo
channel.
Subscriber¶
Important
Make sure to replace xxxx in the domain name in the sample requests below to the one assigned to your application.
Establishing a subscription¶
Request:
curl -H Content-Type:application/json -X POST -d '{ "selector":"city = '\''Dallas'\'' and temperature > 60"}' \
-v https://xxxx.backendless.app/messaging/demo/subscribe
{
"subscriptionId" : "1111-1111-1111-11111"
}
Retrieving messages¶
Request:
curl -X GET
-v https://xxxx.backendless.app/messaging/demo/1111-1111-1111-11111
{
"messages": [
{
"headers": {
"temperature": "62",
"DSId": "FEA0EAD5-51FE-5A8D-FF09-647CD75FCC00",
"state": "Texas",
"DSDstClientId": "63148CEC-9BB2-9B8F-FF4C-FCE56A829A00",
"BL_APPLICATION_ID": "APP-ID",
"city": "Dallas"
},
"publisherId": null,
"data": "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.",
"publishedAt": 1524907478264,
"messageId": "message:2448AE97-8707-CF49-FFE9-9DBCF4B3DC00"
}
]
}
Publisher¶
curl -X POST \
https://api.backendless.com/APP-ID/REST-API-KEY/messaging/demo \
-H 'content-type: application/json' \
-d '{ "message": "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.",
"headers" : {
"city" : "Dallas",
"state" : "Texas",
"temperature":62
}
}'
var message = "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.";
var publishOptions = new Backendless.PublishOptions({
headers: {
"city": "Dallas",
"state": "Texas",
"temperature": 62
}
});
Backendless.Messaging.publish( "demo", message, publishOptions )
.then( function( result ) {
console.log( "message has been published" );
})
.catch( function( error ) {
console.log( "error publishing the message - " + error );
})
String message = "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.";
PublishOptions publishOptions = new PublishOptions();
publishOptions.putHeader( "city", "Dallas" );
publishOptions.putHeader( "state", "Texas" );
publishOptions.putHeader( "temperature", 62);
Backendless.Messaging.publish( "demo",
message,
publishOptions,
new AsyncCallback<MessageStatus>()
{
@Override
public void handleResponse( MessageStatus response )
{
Log.i( "MYAPP", "Message has been published" );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "MYAPP", "Error publishing the message" );
}
} );
let message = "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%."
let publishOptions = PublishOptions()
publishOptions.addHeader(name: "city", value: "Dallas")
publishOptions.addHeader(name: "state", value: "Texas")
publishOptions.addHeader(name: "temperature", value: 62)
Backendless.shared.messaging.publish(channelName: "demo", message: message, publishOptions: publishOptions, responseHandler: { messageStatus in
print("Message has been published")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
NSString *message = @"Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.";
PublishOptions *publishOptions = [PublishOptions new];
[publishOptions addHeaderWithName:@"city" value:@"Dallas"];
[publishOptions addHeaderWithName:@"state" value:@"Texas"];
[publishOptions addHeaderWithName:@"temperature" value:@62];
[Backendless.shared.messaging publishWithChannelName:@"demo" message:message publishOptions:publishOptions responseHandler:^(MessageStatus *messageStatus) {
NSLog(@"Message has been published");
} errorHandler:^(Fault *fault) {
NSLog(@"Error: %@", fault.message);
}];
AsyncCallback<MessageStatus> callback = new AsyncCallback<MessageStatus>(
messageStatus =>
{
Console.WriteLine( $"message has been published {messageStatus.MessageId}");
},
fault =>
{
Console.WriteLine( $"error {fault.Message}");
}
);
PublishOptions publishOptions = new PublishOptions();
publishOptions.AddHeader( "city", "Dallas" );
publishOptions.AddHeader( "state", "Texas" );
publishOptions.AddHeader( "temperature", 62 );
string message = "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%."
Backendless.Messaging.Publish( message,
"demo",
publishOptions,
callback );
String message = "Thunderstorms likely. High 62F. Winds NNW at 10 to 15 mph. Chance of rain 90%.";
PublishOptions publishOptions = new PublishOptions();
publishOptions.headers = {
"city": "Dallas",
"state": "Texas",
"temperature": "62"
};
Backendless.messaging.publish(message,
channelName: "demo",
publishOptions: publishOptions)
.then((response) {
print("Message has been published");
});