Skip to content

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):

Backendless.Messaging.Subscribe( "MyChannel" ).AddMessageListener<String>( message => 
  {
    System.Console.WriteLine( $"message received {message}");
  }
);

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:

Backendless.Messaging.Subscribe().AddMessageListener<Dictionary<string, object>>( message =>
{
  Console.WriteLine( "Received message: ");

  foreach (KeyValuePair<string, object> kvp in message)
    Console.WriteLine( $"Key = {kvp.Key}, Value = {kvp.Value}");
});

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:

Backendless.Messaging.Subscribe().AddMessageListener<Person>( message =>
{
  Console.WriteLine( $"Received message: person's ame: {message.name}, person's age: {message.age}");
});

General signature

A general signature for receiving messages as instances of a custom type:

Backendless.Messaging.Subscribe().AddMessageListener( MessageReceived<T> callback );

where MessageReceived is declared as:

public delegate void MessageReceived<T>( T message );