Backendless Support
 

Basic publish/subscribe messaging

The concept of publish/subscribe messaging is rather simple - a program can publish a message to a queue or a topic, while another program subscribes to the queue or the topic to receive published messages.

There are a lot caveats in the model with conditional delivery, message filtering, message transformations, etc. 

Backendless Messaging is a very powerful piece of technology. It offers a great amount of features providing unique developers' as well as app users' experience.

This topic demonstrates the most basic form of publish/subscribe messaging. One client will be publishing basic string messages, while any number of other client apps can subscribe to receive published messages. 

Consider the following example.

                                           For Objective-C and Swift implementations CLICK HERE

Asynchronous sample (Android and Plain Java):
int i = 1;
while( true )
{
 Backendless.Messaging.publish( "default", "Message " + i++, new AsyncCallback<MessageStatus>()
 {
 @Override
 public void handleResponse( MessageStatus messageStatus )
 {
 System.out.println( "Message published - " + messageStatus.getMessageId() ); 
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error " + backendlessFault.getMessage() );
 }
 } );
 Thread.sleep( 500 );
}
Synchronous sample (Plain Java only):
int i = 1;
while( true )
{
 MessageStatus messageStatus = Backendless.Messaging.publish( "default", "Message " + i++ );
 System.out.println( "Message published - " + messageStatus.getMessageId() );
 Thread.sleep( 500 );
}

Both samples above publish string messages to the "default" channel. A channel is a logical medium carrying the messages. Think of it as a pipe connecting publisher with the subscribers. You can use your own messaging channels in your app - Backendless lets you create any number of channels.

The code below is the subscriber. A developer can run that code in multiple instances of the program, for example, on different devices. Every running instance will be a separate subscriber: 

Backendless.Messaging.subscribe( new AsyncCallback<List<Message>>()
{
 @Override
 public void handleResponse( List<Message> messages )
 {
 Iterator<Message> messageIterator = messages.iterator();
 while( messageIterator.hasNext() )
 {
 Message message = messageIterator.next();
 System.out.println( "Received message - " + message.getData() );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error " + backendlessFault.getMessage() );
 }
} );
The publisher code produces the following output:
Message published - 37243355-2DB1-95F8-FF0A-FC19DEF09D00
Message published - BE150154-B9E1-D95C-FFC4-3ECBDB572300
Message published - 60E00A27-9CDB-A5A3-FF17-E1E45020F300
Message published - F139EF3B-AEB0-51F5-FFD6-74B78065D100
Message published - DA38D9AC-7F00-652C-FFB3-58DCA660BA00
Message published - EC8684CF-EA71-3032-FFAA-9091F6727D00
Message published - 39ADB9ED-61D9-EDC1-FF5C-D88814C6A900
Message published - 7EEC5EEF-95D0-903C-FF39-5A3AA3CAF700
While the subscriber prints out the following:
Received message - Message 40
Received message - Message 41
Received message - Message 42
Received message - Message 43
Received message - Message 44
Received message - Message 45
Received message - Message 46
Received message - Message 47
Received message - Message 48
Received message - Message 49
Is article helpful?