rubymultithreadingxmpp4r

XMPP4R Callbacks dont seem to work


Im using xmpp4r and trying to get the hang of a basic chat feature that I wish to implement later in my Rails app. My fundamentals on Ruby Threads is still a bit shaky so I would appreciate any help on this.

Though I register the callback i dont get a response from my gmail account. I am able to send a message but my ruby program terminates. In order to prevent it from terminating I tried to stop on of the threads in the program but I cant seem to get it working.

require 'rubygems'
require "xmpp4r/client"
require "xmpp4r/roster"

include Jabber

def connect
    client = Client.new(JID::new("test@gmail.com"))
    client.connect
    client.auth("test")
    client.send(Presence.new.set_type(:available))
    client
end

def create_message(message, to_email)

    msg = Jabber::Message::new(to_email, message)
    msg.type = :chat
    msg
end

def subscribe(email_id)
    pres = Presence.new.set_type(:subscribe).set_to(email_id)
    pres
end

client = connect

roster = Roster::Helper.new(client)
  roster.add_subscription_request_callback do |item,pres|
    roster.accept_subscription(pres.from)
end


def create_callback(client)
  $t4= Thread.new do
            client.add_message_callback do |m|
                    puts m.body
                    puts "................................Callback working"
            end
    end

end



  puts "Client has connected"
 msg = create_message("Welcome to the winter of my discontent", "test@tech.com")
 client.send(msg)
 create_callback(client)
 def check(client)
      $t3 =  Thread.new do
            loop do

                    puts "t3 still running........."
                    Thread.current.stop
                    $t4.join
                    end
            end
end

check(client)

Solution

  • Instead of the check(client) stuff you can just use Thread.stop like the XMPP4R examples do.

    If you'd like to see what's going on at the XMPP level, use Jabber::debug = true to enable console debug output.