Receiving Unfiltered Messages¶
Backendless Real-Time Messaging delivers messages to listeners added to a channel. A listener is a callback function which is invoked by Backendless to deliver a message to the client application. Depending on the API used to add a listener, it can receive either all messages published into the channel or a filtered subset of messages. The filtering options are explained in greater detail in the Receiving Filtered Messages section of this guide.
Receiving String Messages¶
Adding a listener to receive messages as string objects. Messages should be published as string objects):
var channel = Backendless.Messaging.subscribe( "MyChannel" );
function onMessage( stringMessage ) {
console.log( "Message received: " + stringMessage.data )
}
channel.addMessageListener( onMessage );
Receiving Dictionary/Map messages¶
Adding a listener to receive messages as dictionary/map objects. Messages can be published as JSON objects, complex types or disctionary/map objects:
var channel = Backendless.Messaging.subscribe( "MyChannel" );
function onMessage( message ) {
console.log( "Message received" );
console.log( "Property x " + message.data.x )
console.log( "Property y " + message.data.y )
}
channel.addMessageListener( onMessage );
Receiving custom type/class messages¶
Adding a listener to receive messages as instances of a custom type (the Person
class). Published messages must be instances of the class, map/dictionary objects or JSON objects with the same structure as the class:
var channel = Backendless.Messaging.subscribe( "MyChannel" );
function onMessage( message ) {
console.log( "Message received" );
console.log( "Property name " + message.data.name )
console.log( "Property age " + message.data.age )
}
channel.addMessageListener( onMessage );
General signature¶
A general signature for receiving messages as instances of a custom type:
// channel object must be obtained via a subscription call, for example:
// var channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( function( message ) { } );