Skip to content

Publish Map/Dictionary

The example demonstrates publishing a value of an untyped data structure and creating a subscriber to receive objects which contain properties of the published object. 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 '{}' \
-v https://xxxx.backendless.app/messaging/demo/subscribe
Response:
{
  "subscriptionId" : "1111-1111-1111-11111"
}

Retrieving messages

Request:

curl -X GET 
-v https://xxxx.backendless.app/messaging/demo/1111-1111-1111-11111
Response:
{
  "messages": [
    {
      "headers": {
        "DSId": "6523B3EE-FF53-8A3F-FF81-984B64A09B00",
        "DSDstClientId": "539D97CD-BA01-4B62-FF70-F95587987E00",
        "BL_APPLICATION_ID": "APP-ID"
      },
      "publisherId": null,
      "data": {
        "name": "Joe",
        "age": 30
      },
      "publishedAt": 1524682836224,
      "messageId": "message:04C39082-AF7F-342F-FF99-E3F71D36E700"
    }
  ]
}

Publisher

curl -X POST \
  https://api.backendless.com/APP-ID/REST-API-KEY/messaging/demo \
  -H 'content-type: application/json' \
  -d '{  "message": { "name":"Joe", "age":30}}'
var personObj = { name:"Joe", age:30 };

Backendless.Messaging.publish( "demo", personObj )
 .then( function( result ) {
    console.log( "message has been published" );
  })
 .catch( function( error ) {
    console.log( "error publishing the message - " + error );
  })
HashMap personObject = new HashMap();
personObject.put( "name", "Joe" );
personObject.put( "age", 30 );

Backendless.Messaging.publish( "demo", 
                               personObject, 
                               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 person = ["name": "Joe", "age": 30] as [String : Any]

Backendless.shared.messaging.publish(channelName: "demo", message: person, responseHandler: { messageStatus in
    print("Message has been published")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
NSDictionary *person = @{@"name": @"Joe", @"age": @30};

[Backendless.shared.messaging publishWithChannelName:@"demo" message:person responseHandler:^(MessageStatus *messageStatus) {
    NSLog(@"Message has been published");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Dictionary<string,object> person = new Dictionary<string,object>();
personObject["name"] = "Joe";
personObject["age"] = 30;

AsyncCallback<MessageStatus> callback = new AsyncCallback<MessageStatus>(
   messageStatus =>
   {
     Console.WriteLine( $"message has been published {messageStatus.MessageId}"); 
   },
   fault =>
   {
     Console.WriteLine( $"error {fault.Message}");
   } 
);

Backendless.Messaging.Publish( "demo", 
                               person, 
                               callback );
Map personObject = {
  "name": "Joe",
  "age": 30
};
Backendless.messaging.publish(personObject, channelName: "demo").then((response) {
  print("Message has been published");
});