Receiving Messages

You can oberserve all incoming messages by subscribing to the XmppXElementStreamObserver instance. Using Linq we tell our observer that we are only interested in XMPP stanzas.

Subscribe to all incoming message stanzas:

xmppClient
    .XmppXElementStreamObserver
    .Where(el => el is Message)
    .Subscribe(el =>
    {
        // handle the message here
        Debug.WriteLine(el.ToString());
    });

Subscribe to all incoming chat message stanzas

In this example we subscribe to all stanzas which are of type chat. This represents 1:1 chat messages in XMPP

xmppClient
    .XmppXElementStreamObserver
        .Where(el =>
            el.OfType<Message>()
            && el.Cast<Message>().Type == MatriXMessageType.Chat)
    .Subscribe(el =>
    {
        // handle the message here
        Debug.WriteLine(el.ToString());
    });

Subscribe to all incoming group chat message stanzas

In this example we subscribe to all stanzas which are of type groupchat. This represents messages form chat rooms in XMPP

xmppClient
    .XmppXElementStreamObserver
        .Where(el =>
            el.OfType<Message>()
            && el.Cast<Message>().Type == MatriXMessageType.GroupChat)
    .Subscribe(el =>
    {
        // handle the message here
        Debug.WriteLine(el.ToString());
    });

Subscribe to all incoming messages form a given chat room

This example adds another condition which is evalating the messages to match a specific Jid

xmppClient
    .XmppXElementStreamObserver
      .Where(el => 
            el.OfType<Message>()
            && el.Cast<Message>().Type == MessageType.GroupChat
            && el.Cast<Message>().From.Equals(new Jid("dev@donference.ag-software.net"), new BareJidComparer()))
    .Subscribe(el =>
    {
        // handle the message here
        Debug.WriteLine(el.ToString());
    });

As you can see RX, Linq and predicates offer us huge flexibility here to filter incoming stanzas and process the different available types.