Skip to content

Publish Strings

The example demonstrates publishing a string value and creating a subscriber for string messages. The message is published into the demo channel.

Subscriber

Channel channel = Backendless.Messaging.subscribe( "demo" );
channel.addMessageListener( new AsyncCallback<String>()
{
  @Override
  public void handleResponse( String message )
  {
    Log.i( "MYAPP", "Received a message " + message );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Error processing a message " + fault );
  }
} );

Publisher

curl -X POST \
  https://api.backendless.com/APP-ID/REST-API-KEY/messaging/demo \
  -H 'content-type: application/json' \
  -d '{  "message": "It's a great day!" }'
Backendless.Messaging.publish( "demo", "It's a great day!" )
 .then( function( result ) {
    console.log( "message has been published" );
  })
 .catch( function( error ) {
    console.log( "error publishing the message - " + error );
  })
Backendless.Messaging.publish( "demo", 
                               "It is a great day!", 
                               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" );
  }
} );
Backendless.shared.messaging.publish(channelName: "demo", message: "It is a great day!", responseHandler: { messageStatus in
    print("Message has been published")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
[Backendless.shared.messaging publishWithChannelName:@"demo" message:@"It is a great day!" 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}");
   } 
);

Backendless.Messaging.Publish( "demo", 
                               "It is a great day!", 
                               callback );