So I built what is an instant messaging bot using the Smack API and the Openfire XMPP server. Doing it is pretty simple of you read the documentation and Smack’s developer notes. The bot relies on a queue-like object that Smack gives you called a PacketCollector. All you need to do is log in to the server, and let the packet collector wait for incoming messages. Clearly, you need to respond to the messages and use thread tools to do the waiting and all, but in general, the effort is relatively straightforward.
Tag: JSJaC
Setting up JSJaC with Openfire 3.5.2
I am using Openfire, an XMPP (Jabber) server, to facilitate instant messaging in a closed system I am building. Part of my design had users rely on browsers as instant messaging (IM) clients. As Meebo showed, it can be done quite well and an open source JavaScript library, JSJaC, apparently provides this ability. JSJaC is the backbone of a full-blown browser-based IM client called JWChat, but for me, the plumbing was what was necessary.
Only thing is that JSJaC is not the easiest to set up with Openfire.
JSJaC uses HTTP-binding, which according to Wikipedia allows for long-lasting HTTP requests instead of polling, which just periodically checks the server to see if there is a message waiting. To do that, JSJaC needs to connect to the server and that is done by accessing a somewhat tricky port, 7070 is the default in version 3.5.2. What’s tricky about it? Browsers do not like to let JavaScript access services using XMLHttpRequest, the lifeblood of AJAX, on URLs and ports unlike the one which the current URL relies on. This is normally solved using a proxy or a URL rewrite, which forward requests to the desired ports and pass back responses, all as if you used the ‘appropriate’ ports. Luckily, Apache’s HTTPd has great modules that do just that. The instructions in JSJaC’s readme file recommended using a rewrite. That caused repeated 503 errors from Apache. Using its proxy did work for me. As usual, you spend time, read here and read there, and finally, you normally find a way to make it work. This how I did it.
- After downloading JSJaC, open jsjac.js. Change the line
var JSJACHBC_USE_BOSH_VER=false;
to
var JSJACHBC_USE_BOSH_VER=true;
- To allow Apache HTTPd’s proxy to work, enable
proxy_module
andproxy_http_module
in Apache HTTPd’shttpd.conf
file - Add a virtual host that will map a certain URL to the proxy, and set up the proxy to point at the Openfire http-binding port:
<virtualhost 127.0.0.1:80> DocumentRoot "C:/www/root" <!-- This is the document root for your server --> AddDefaultCharset UTF-8 ProxyRequests On ProxyPass /http-bind/ http://127.0.0.1:7070/http-bind/ <!-- This is where your Openfire server can be accessed --> </virtualhost>
- To test this configuration, use JSJaC’s simpleclient.html, its sample client, to try and connect to the server. Make sure you add the following line
oArgs.authtype = 'nonsasl';
above
con.connect(oArgs);
- Start Apache HTTPd and open simpleclient.js
-
simpleclient asks for 4 fields:
- The server’s base URL: This is the proxied URL, for example, http://127.0.0.1/http-bind/ (note the trailing slash) in my case.
- Server name/domain: The domain Openfire was set up to use. In XMPP, if the server’s name is myserver, users will be known on it with IDs like joe@myserver
- User name of existing user
- The password for that user
- Connect!
I highly recommend using Firebug to ensure the request is sent properly and is being received properly from Openfire. Odd Firefox object initialization errors are due to the fact that an attempt is made to access a port JavaScript is not allowed to access.