0
votes

What is the proper xmpp4r way to know if a given contact is online before sending them a message?

Can you post sample xmpp4r code for doing this?

Here is my use case:

  1. If contact online, send :normal message

  2. Else, email contact

Here are things I have working code for:

  1. Send messages of various types

  2. Get a roster/contact list

  3. Register a call back to detect changes in presence

However, I can't find a place that directly addresses a work flow like this:

  1. Loop through each JID in your roster

  2. If jid.is_online? == true, send IM

  3. Else, send email

I've read that you should send a JID a message of type :headline and if that fails, you know the user is offline. In my tests, if the user is ONLINE, they'll receive a message of type headline. This is suboptimal, as users should only receive messages to read, not noise to determine online status.

I've read that on sign on, all of your contacts will bounce a presence status back at you, and that status is the sole indication that they are online - assuming that there isn't a disconnect or presence change you've yet to receive. So you should register a presence call back, record the initial users who ping you back, and then add or remove from the list based on your running roster presence callback.

If this is truly the way to do it:

  1. Can I get some example code of how to collect all the "I'm here" presence confirmations on sign on via xmpp4r?

  2. Why, oh why, was xmpp designed this way and why is this better than offering an "is_online_and_available" method?

1

1 Answers

0
votes

So the answer here is adding a message call back and checking inside the block for the type:

m = Message.new(to, body)
cl.send(m)

cl.add_message_callback do |m|
  if m.type == :error
    puts "type: #{m.type}"
  else
    puts "not an error"
  end
end

This requires threading as you have to be listening for the response.