PHP Code, Setup and Demo of Jaxl boshchat application

Standard

Jaxl 2.0 bosh support allow web developers to write real time web applications within minutes, without having any pre-requisite knowledge about the XMPP protocol itself. In this blog post, I will walk you through setup and demo of an XMPP based web chat application using Jaxl library.

Get the code
Follow the following steps to download and install this sample web application on your systems:

  • Clone the development branch of Jaxl library
    root@ubuntu:~/git# git clone git@github.com:abhinavsingh/JAXL.git
    root@ubuntu:~/git# cd JAXL/
    root@ubuntu:~/git/JAXL#

    If you are not familiar with git, simply visit JAXL@github, click Download Source and extract under ~/git/JAXL directory on your system

  • Once inside Jaxl source directory, build the latest development package
    root@ubuntu:~/git/JAXL# ./build.sh
    building...
  • Install Jaxl library (view installation detail and options)
    root@ubuntu:~/git/JAXL# ./build.sh install
    uninstalling old package...
    installing...

Setup web chat application
Jaxl library is default installed under /usr/share/php/jaxl folder. Application code for our web chat application can be found under /usr/share/php/jaxl/app/boshchat folder.

Follow these steps to setup web chat application on your system:

  • I assume you have http://localhost/ configured on your local web server and it runs out of /var/www folder. Create following symlinks:
    root@ubuntu:~/git/JAXL# cd /var/www
    root@ubuntu:/var/www# ln -s /usr/share/php/jaxl/app/boshchat/boshchat.php index.php
    root@ubuntu:/var/www# ln -s /usr/share/php/jaxl/app/boshchat/jaxl.ini jaxl.ini
    root@ubuntu:/var/www# ln -s /usr/share/php/jaxl/env/jaxl.js jaxl.js
    root@ubuntu:/var/www# ln -s /usr/bin/jaxl jaxl.php

    Edit/Remove #!/usr/bin/env php inside jaxl.php if it causes any problem on your system.

  • Open and edit jaxl.ini
    define('JAXL_BOSH_COOKIE_DOMAIN', false);
  • I assume you have access to XMPP over Bosh enabled jabber server. Ejabberd users can verify this by hitting http://localhost:5280/http-bind in the browser
  • Open and edit index.php
    define('BOSHCHAT_ADMIN_JID', 'admin@localhost');

    All messages sent using this web chat application will be routed to BOSHCHAT_ADMIN_JID

Ready for the demo
To run this example web chat application, visit http://localhost in your browser window. Enter a username/password already registered on your jabber server and press connect.

Login as BOSHCHAT_ADMIN_JID using a desktop client, so that you can receive messages sent from the browser on your desktop client.

Below is a screenshot when I logged in as “abhinavsingh” from browser and BOSHCHAT_ADMIN_JID was set to “jaxl@jaxl.im”:

  • Tomas

    Hello Abhi,

    you wrote, that you’re gonna wrote something about installation on Windows server, how is it going? :-)

    I’m just new in JAXL, I don’t know where to start and it would be nice, if you wrote it for beginners like me.

    Thx for response.

  • Harshad

    Hello Abhinav,

    I have tried your library and it works great.
    I have a issue I need your help with, when I try the demo boshchat client from apps folder it works great, but when I try to send message using only php and no javascript it doesn’t work, I want to send message to multiple uses in a loop using php but can’t get it to work, can you please tell me what should I do in order to make in work without JavaScript, my ejabberd logs show that jaxl connects to server just fine but message is not delivered.

    Thanks and Looking for your response.

    • http://abhinavsingh.com Abhinav Singh

      you should send messages in one single go…. i.e. you can create several packets, but send them in one go. This way Jaxl won’t break the php session locking required to work from browser apps (i suppose this is that must be causing issue for you if you tried to send multiple packet in a loop).

      You can do it in two ways: Either by directly using XMPPSend::message($jaxl, …..) where every other parameter to this function should be an array(). Size of array equals to number of packets you are trying to send. Alternately, you can use XMPPSend::prepareMessage($jaxl, ….) to create raw Xml packets on by one. When you are done creating them all, you can directly send the xml packet over the wire.

  • Antonio

    Hello Abhinav,
    first thanks for your work.
    I’m testing your webchat with bosh and it works well.
    But in the page that containing it, I would like to show the presence of admin who receive message from webchat.
    How can I do?
    In boshchat class there is a function called getPresence, that I think it display when an users do login/logoff from the webchat. So it isn’t good (and doesn’t work).
    Thanks.

    • http://abhinavsingh.com Abhinav Singh

      basically you need to login user into an account, which receives admin presence info on signup. Alternately you can send a presence probe stanza to check if admin is online/offline and display the info accordingly.

  • Harshad

    Hello Abhinav, I have made some changes to boshChat.php so that it can connect with existing jaxl session, but after reconnecting when I try to pass the jaxl id I receive payload is empty error:

    Here is the code
    case ‘reconnect':
    {
    $response = array(‘jaxl’=>’reconnected’, ‘jid’=>$jaxl->jid);
    $jaxl->JAXL0206(‘out’, $response);
    break;
    }

    Thanks

  • anamika saxena

    dear Abhinav,

    hi, i m anamika. I have a problem about chatting that how only 2 pesons can chat and other can’t……plz tell me n i hv downloaded ur Jaxl boshchat application, but i couldn’t find that prob. which i hv mentioned above. plzz help me..

  • stella0302

    Hi,

    Thanks for good work!

    I would like to use facebook chat function on my web app. My questions are:

    1. I have a php script that connects to facebook chat server to do the authentication,send/receive msg. How to push/receive msgs to/from the web client? Is using bosh the only way to achieve this goal? Is there any way that I can let other php scripts share this socket?

    2. In your jaxl bosh chat app, who is the http cm, who is the server? Is the php on the local host a bosh server, or is XMPP server a bosh server, the php on your localhost is a http CM?

    Look forward your kind reply!

  • German

    Hi,
    I have a problem about authentication in chat, but in my case, I have openfire server running in VirtualBox, I can connect to it but in the conexion, the server don’t authenticate my user, shown in the log

    and my user is register in openfire server, an answer?

    PD: sorry, my english is bad!! XD

  • German

    Sorry, shown in the log =
    “”

  • http://indabamobile.co.za Duma

    I would like to know if, with JAXL, I can programmatically logon and set the MOTD (mod_announce) on an ejabberd server.

    I am building an Admin console in php and cannot find a tutorial anywhere on how to do this.

    Please point me in the right direction.

    Duma

  • http://inveera.com shrikant

    Dear Abhinav Sir,
    I have download this application and when i was run it http://localhost/jaxl/app/boshchat.php
    then login window show and it sends a request to boshchat.php

    and it gives the following error
    touch() [function.touch]: Unable to access /var/log/jaxl.log in D:\xampp\htdocs\jaxl\core\jaxl.class.php on line 693
    Kindly give me solution How I run this application

    • http://abhinavsingh.com Abhinav Singh

      you need right permissions…. jaxl is unable to create a log file… try to pass `’logPath’=>’/tmp/jaxl.log’` which should get this fixed for you…. alternately update the permissions as required

  • Carlos Perez

    Hi. I have a problem with Jaxl. I created a class for a boshchat client based on the code of the boshchat example. Basically it works, I can authenticate and send and receive messages for a while.

    The problem is that after a couple of minutes the jaxl object from jaxl.js starts sending continuous ping requests and chat stops working. Can’t send or receive any more messages.

    I checked the log file and found that ‘sid’ is missing and it reports “<body type='terminate' condition='improper-addressing' …

    I'm including a fragment of the log file, so you can see when it happens.

    Thanks and look forward your reply.

    [1:2851:0] 2012-05-18 13:48:46 – [[XMPPSend]] body

    [1:2849:0] 2012-05-18 13:48:47 – [[XMPPGet]]

    [1:2850:0] 2012-05-18 13:48:47 – [[XMPPSend]] body

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPGet]]

    [1:2852:0] 2012-05-18 13:49:17 – [[XMPPSend]] body

    [1:2852:0] 2012-05-18 13:49:17 – [[XMPPGet]]

    [1:2850:0] 2012-05-18 13:49:17 – [[XMPPGet]]

    [1:2849:0] 2012-05-18 13:49:17 – [[XMPPSend]] body

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPSend]] body

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPGet]]

  • Carlos Perez

    Hi. Looking at the first version of Jaxl I tested, 2.1.2rc1, found that disconnection happens exactly a minute after the first ping starts. With 2.1.2rc2 is random, just a time between 1 and 2 minutes after the first ping request.

    I’m using JAXL0206 like in boshchat.php to keep the connection alive, but also tried with JAXL0199. With that one only got continous ping requests.

    I included a piece of the jaxl.log file before, but was not right. Will include it again.

    Thanks in advance.

    [1:2851:0] 2012-05-18 13:48:46 – [[XMPPSend]] body
    body rid=”4160″ sid=”d6f58abe46c1f8ce3b9f48ac70d215576076a615″ xmlns=”http://jabber.org/protocol/httpbind”/

    [1:2849:0] 2012-05-18 13:48:47 – [[XMPPGet]]
    body xmlns=’http://jabber.org/protocol/httpbind’/

    [1:2850:0] 2012-05-18 13:48:47 – [[XMPPSend]] body
    body rid=”9763″ sid=”3b5c8ca5ae4f930dab2365924976bbeaea00b3fb” xmlns=”http://jabber.org/protocol/httpbind”/

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPGet]]
    body xmlns=’http://jabber.org/protocol/httpbind’/

    [1:2852:0] 2012-05-18 13:49:17 – [[XMPPSend]] body
    body rid=”8228″ sid=”” xmlns=”http://jabber.org/protocol/httpbind”/

    [1:2852:0] 2012-05-18 13:49:17 – [[XMPPGet]]
    body type=’terminate’ condition=’improper-addressing’ xmlns=’http://jabber.org/protocol/httpbind’/

    [1:2850:0] 2012-05-18 13:49:17 – [[XMPPGet]]
    body xmlns=’http://jabber.org/protocol/httpbind’/

    [1:2849:0] 2012-05-18 13:49:17 – [[XMPPSend]] body
    body rid=”9764″ sid=”3b5c8ca5ae4f930dab2365924976bbeaea00b3fb” xmlns=”http://jabber.org/protocol/httpbind”/

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPSend]] body
    body rid=”8229″ sid=”” xmlns=”http://jabber.org/protocol/httpbind”/

    [1:2851:0] 2012-05-18 13:49:17 – [[XMPPGet]]
    body type=’terminate’ condition=’improper-addressing’ xmlns=’http://jabber.org/protocol/httpbind’/

  • Carlos Perez

    Hi again :-)

    Excuse me for sending a message again. Here is ejabberd’s log file. You can see the disconnection because of a session timeout. It looks like ejabberd server doesn’t see http_poll requests and session expires. I also modified the timeout at the server, but now it closes connection after 2 minutes instead of 1 even when I set timeout to 5 minutes.

    =INFO REPORT==== 2012-07-05 12:16:16 ===
    I(:ejabberd_listener:232) : (#Port) Accepted connection {{127,0,0,1},49148} -> {{127,0,0,1},5280}

    =INFO REPORT==== 2012-07-05 12:16:16 ===
    I(:ejabberd_http:138) : started: {gen_tcp,#Port}

    =INFO REPORT==== 2012-07-05 12:16:35 ===
    I(:ejabberd_http_bind:508) : Session timeout. Closing the HTTP bind session: “9b4cfcc9730fc862a6e4be6d0f39245fa07e1d74″

    =INFO REPORT==== 2012-07-05 12:16:35 ===
    I(:ejabberd_c2s:1409) : ({socket_state,ejabberd_http_bind,{http_bind,,{{127,0,0,1},49038}},ejabberd_http_bind}) Close session for charlie@localhost/jaxl.1.1341483271

    I also installed Openfire to test a different server. It takes longer to disconnect, but in the end it does. With Firefox and Chrome takes longer, but with opera disconects in a few seconds.

    This is the message in Openfire’s log file when server disconnects client.

    2012.07.05 14:57:25 org.jivesoftware.openfire.http.HttpBindServlet – Client provided invalid session: . [127.0.0.1]

  • http://abhinavsingh.com Abhinav Singh

    Announcing Jaxl v3.x – asynchronous, non-blocking I/O, event based PHP client/server library – http://abhinavsingh.com/blog/2012/07/announcing-jaxl-v3-x-asynchronous-non-blocking-io-event-based-php-clientserver-library/