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):
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((String stringMessage) {
print("Received string message: $stringMessage");
});
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:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((Map message) {
print("Received message as a map: $message");
});
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:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((Person message) {
print("Received message as a Person object: $message");
});
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:
// Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener<T>(void callback(T response));
Receiving messages in Backendless internal format¶
Adding a listener to receive message in the Backendless internal format:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((PublishMessageInfo message) {
print("Published message - ${message.message}");
print("Publisher ID - ${message.publisherId}");
print("Message headers - ${message.headers}");
print("Message subtopic ${message.subtopic}");
});