Skip to content

Publish Custom Type

The example demonstrates publishing a value of a custom type (Person) and creating a subscriber to receive objects of that type. The message is published into the demo channel.

Subscriber

Person class

@interface Person : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber *age;

@end

Subscriber code

Channel *channel = [Backendless.shared.messaging subscribeWithChannelName:@"demo"];
RTSubscription *subscription = [channel addCustomObjectMessageListenerForClass:[Person class] responseHandler:^(Person *message) {
    NSLog(@"Received a message: %@", message);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Person class

@objcMembers class Person: NSObject {
    var name: String?
    var age: NSNumber?
}

Subscriber code

let channel = Backendless.shared.messaging.subscribe(channelName: "demo")
let _ = channel.addCustomObjectMessageListener(forClass: Person.self, responseHandler: { message in
    print("Received a message: \(message)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

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}}'
function Person() {

}

var personObj = new Person();
personObj.name = "Joe";
personObj.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 );
  })
public class Person
{
  // can also use getter/setter methods 
  // instead of public fields
  public String name;
  public int age;
}

Person personObject = new Person();
personObject.name = "Joe";
personObject.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 = Person()
person.name = "Joe"
person.age = 30

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

[Backendless.shared.messaging publishWithChannelName:@"demo" message:person responseHandler:^(MessageStatus *messageStatus) {
    NSLog(@"Message has been published");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
public class Person
{
  // can also use get/set properties 
  // instead of public fields
  public string name;
  public int age;
}

Person personObject = new Person();
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", 
                               personObject, 
                               callback );
@reflector
class Person {
  String name;
  int age;
}

Person personObject = new Person()
  ..name = "Joe"
  ..age = 30;
Backendless.messaging.publish(personObject, channelName: "demo").then((response) {
  print("Message has been published");
});