You are currently browsing all posts tagged with 'jabber'.

Experiences with flash and ejabberd (II)

  • February 7, 2009 1:03 pm

When I was writing Flash and ejabberd experiences article, I thought to include all the detailed steps but I was afraid of repeating once more all the steps. However, after having some doubts whether it was really necessary to apply a patch to ejabberd I think it’s worth to review some of them.

Below are the steps I followed to test a chat example.

  1. install latest SVN XIFF version (Checked out revision 10959)
    export TEST_DIR=/tmp/xiffstep1
    mkdir -p $TEST_DIR/xiff
    cd  $TEST_DIR/xiff
    svn co http://svn.igniterealtime.org/svn/repos/xiff/trunk/src/org org
  2. install latest SVN ejabberd version (2.0.2)
    cd  $TEST_DIR
    svn co http://svn.process-one.net/ejabberd/tags/ejabberd-2.0.2/ ejabberd_src
    cd $TEST_DIR/ejabberd_src/src
    ./configure --prefix=$TEST_DIR/ejabberd
    make
    sudo su
    make install
  3. setup ejabberd and create two users (sender, receiver)
    cd $TEST_DIR/ejabberd/sbin
    ./ejabberdctl start
    ./ejabberdctl register user1 localhost user1
    ./ejabberdctl register user2 localhost user2
  4. get chat example files: xiffstep1.mxml and TestChat.as. Note this example is based on this article.
    cd $TEST_DIR/xiff
    wget http://www.keoko.net/wp-content/uploads/2009/02/xiffstep1.mxml
    wget http://www.keoko.net/wp-content/uploads/2009/02/testchat.as
  5. create AS3 project “Test1″ with eclipse + Flex Builder, import library XIFF and example file into the project and run the application.
    # run eclipse
    File > New > Flex Project -> Project Name: XIFFstep1
    Delete > XIFFstep1.mxml
    File > Import > General > File System > Next > From directory: $TEST_DIR/xiff
    Run > Run
  6. send message through the flash application in the browser window that has been opened.
  7. check XML/jabber communication.
    • Request:
      <policy-file-request/>
    • Response:
      <?xml version='1.0'?>
      <stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='3095424478' from='localhost' xml:lang='en'>
      <stream:error>
      <invalid-namespace xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
      </stream:error>
      </stream:stream>

As pointed out by Mickaël Rémond in this comment, XIFF is not XMPP compliant. It’s clear on the XML request. However, I still don’t see how to connect XIFF with ejabberd without applying the patch mentioned on the first part. BOSH seems the only alternative.

Any suggestion?

XMPP PubSub with ejabberd and XMPP4R

  • December 17, 2008 3:12 pm

After reading “Beyond REST? Building Data Services with XMPP PubSub” and other articles about PubSub with XMPP, I decided it would be worth to test it. However, I didn’t find any complete step by step guide in how to test it with ejabberd as XMPP server and XMPP4R as XMPP client.

Below are the steps I followed to test a subscriber (user “sub”) waiting for items published to the node (”home/localhost/pub/updates”) for the publisher (user “pub”). Note you can add as many subscribers/publishers as you want.

  1. install ejabberd (XMPP server). I followed instructions of this article (spanish) or article (english) without any surprise.
  2. create two ejabberd users: “pub” (the publisher) and “sub” (the subscriber).
    sudo ejabberdctl register pub localhost pub
    sudo ejabberdctl register sub localhost sub 
  3. install XMPP4R Ruby gem.
    sudo gem install xmpp4r
  4. create file nodecreator.rb. See code below.
    #! /usr/bin/ruby
    require "rubygems"
    require "xmpp4r"
    
    require "xmpp4r/pubsub"
    require "xmpp4r/pubsub/helper/servicehelper.rb"
    require "xmpp4r/pubsub/helper/nodebrowser.rb"
    require "xmpp4r/pubsub/helper/nodehelper.rb"
    
    include Jabber
    Jabber::debug = true
    
    service = 'pubsub.localhost'
    jid = 'pub@localhost/laptop'
    
    password = 'pub'
    client = Client.new(JID.new(jid))
    client.connect
    client.auth(password)
    
    client.send(Jabber::Presence.new.set_type(:available))
    pubsub = PubSub::ServiceHelper.new(client, service)
    pubsub.create_node('home/localhost/pub/')
    pubsub.create_node('home/localhost/pub/updates')
    
  5. create file publisher.rb. See code below.
  6. #! /usr/bin/ruby
    require "rubygems"
    
    require "xmpp4r"
    require "xmpp4r/pubsub"
    require "xmpp4r/pubsub/helper/servicehelper.rb"
    require "xmpp4r/pubsub/helper/nodebrowser.rb"
    require "xmpp4r/pubsub/helper/nodehelper.rb"
    include Jabber
    Jabber::debug = true
    jid = 'pub@localhost/laptop'
    
    password = 'pub'
    service = 'pubsub.localhost'
    node = 'home/localhost/pub/updates'
    # connect XMPP client
    client = Client.new(JID.new(jid))
    # remove "127.0.0.1" if you are not using a local ejabberd
    client.connect("127.0.0.1")
    client.auth(password)
    client.send(Jabber::Presence.new.set_type(:available))
    # create item
    pubsub = PubSub::ServiceHelper.new(client, service)
    item = Jabber::PubSub::Item.new
    xml = REXML::Element.new("greeting")
    xml.text = 'hello world!'
    
    item.add(xml);
    # publish item
    pubsub.publish_item_to(node, item)
    
  7. create file subscriber.rb. See code below.
    
    #! /usr/bin/ruby
    require "rubygems"
    require "xmpp4r"
    require "xmpp4r/pubsub"
    require "xmpp4r/pubsub/helper/servicehelper.rb"
    
    require "xmpp4r/pubsub/helper/nodebrowser.rb"
    require "xmpp4r/pubsub/helper/nodehelper.rb"include Jabber
    #Jabber::debug = true
    jid = 'sub@localhost/laptop'
    password = 'sub'
    node = 'home/localhost/pub/updates'
    service = 'pubsub.localhost'
    
    # connect XMPP client
    client = Client.new(JID.new(jid))
    # remove "127.0.0.1" if you are not using a local ejabberd
    client.connect("127.0.0.1")
    client.auth(password)
    client.send(Jabber::Presence.new.set_type(:available))
    sleep(1)
    # subscribe to the node
    pubsub = PubSub::ServiceHelper.new(client, service)
    pubsub.subscribe_to(node)
    subscriptions = pubsub.get_subscriptions_from_all_nodes()
    puts "subscriptions: #{subscriptions}\n\n"
    puts "events:\n"
    
    # set callback for new events
    
    pubsub.add_event_callback do |event|
    begin
    event.payload.each do |e|
    puts e,"----\n"
    end
    rescue
    puts "Error : #{$!} \n #{event}"
    
    end
    # infinite loop
    loop do
    sleep 1
    end
    
  8. run nodecreator.rb. It creates the XMPP node “home/localhost/pub/updates”. It creates first the node “home/localhost/pub” and then the “home/localhost/pub/updates”. Seems quite obvious but I spent some hours after I got it.
  9. check the nodes have been created. I used the discovery service functionality of Psi client.

    psi_service_discovery

  10. run subscriber.rb file. The “sub” user subscribes to the node ‘updates’ and waits for items in the “updates” node. Be aware you should close any XMPP connection with users “pub” and “sub” in case you are using any XMPP client such as Pidgin, Psi,… otherwise it won’t work.
  11. run publisher.rb file. It will send a message “<greetings>hello world!”</greetings> to the subscriber. Run it as many times as you want.
  12. If everything goes well yo will see in the subscriber screen a message like this.
    
    subscriptions: <subscription node='home/localhost/pub/updates' jid='sub@localhost' subscription='subscribed' xmlns='http://jabber.org/protocol/pubsub'/>
    
    events:
    <items node='home/localhost/pub/updates'><item id='3376'><greeting>hello world!</greeting></item></items>
    ----
    

Good Luck!

For this test, I used the following versions:

  • Operating System: Ubuntu 8.04
  • Ruby: 1.8.6
  • XMPP4R: 0.4
  • ejabberd: 1.1.4