Mridang Agarwalla
Mridang Agarwalla

Reputation: 45018

XMPP based live-chat system in Django

I need to implement an XMPP based live-chat system in Django. After a lot of scourging and ideas from a colleague, we came up with this.


Method using a bot:

ejabberd or openfire will be XMPP server. Apache is the web server which runs mod_wsgi for serving Django and mod_proxy for proxying the XMPP-over-BOSh requests to the XMPP server.

Does this sound like a good of doing this? Any suggestions? I'm worried about the load on the Django system.

(It's long. Sorry 'bout that.)


Method using Presence Stanzas:

On the client side, i'm using Strophe JS library which supports presence and I had add callback methods. I'm flexible with using ejabberd or openfire as my XMPP server. The are many visitors on the XMPP server — some from site A and some from site B but they are all connected to the same XMPP server. When the visitor visits the site, they are connected to the XMPP server as <visitor_id>_<site_id>@demo.com and each one gets logged into a room called <visitor_id>@conference.demo.com. The sales/support personnel are also connected to the XMPP sever as <supportsale_id>_<site_id>@demo.com. They are not connected to any chat room though. They don't have any of the visitors on their roster.

A good way of showing that a user has connected to the site would be to pass a presence stanza to the sales/support people. Only visitors and sale/support personnel from the same site communicate with each other and that's why I have the <site_id> in the username to show which site that person belongs to.

It seems that you can't subscribe to presence stanzas for a user if you don't have him on your roster. (Quite logical). Is is possible to automatically add every new user of a site connecting to the system to the roster of the sales/support people of that site? Wouldn't this then automatically signal a presence to the sales/support people? How can I implement this — any help?

Upvotes: 4

Views: 6584

Answers (3)

Gjorgji Tashkovski
Gjorgji Tashkovski

Reputation: 1983

I think that it's better to use presence stanzas to "signal" any (in)activity. What you need to store in database is only the persistent data you need for further analysis. Otherwise, I think you'll have great time coding the application :).

EDIT:

function onConnect(status) {
  if (status == Strophe.Status.CONNECTED) {
    var joined = false;
    var participants = {};
    $('#events').html('<text class="textmainleft">XMPP connection established. Ready to rock n roll!</text>');
    connection.send($pres().c('priority').t('-1'));
    connection.addHandler(notifyUser, null, 'message', 'groupchat', null, null);
    connection.send($pres({to: '[email protected]/' + nickname}).c('x', {xmlns: 'http://jabber.org/protocol/muc'}));
  } else if (status == Strophe.Status.AUTHFAIL) {
    $(location).attr('href', AUTHFAIL_URL);
  } else if (status == Strophe.Status.CONNFAIL) {
    $(location).attr('href', AUTHFAIL_URL);
  }
}

$(document).ready(function () {
  connection = new Strophe.Connection(BOSH_SERVICE);
  connection.connect(jid, password, onConnect);
});

notifyUser is another function (just link onConnect) that would handle the received message stanzas.

Upvotes: 1

Kirk Strauser
Kirk Strauser

Reputation: 30947

I wrote exactly this. It's called Seshat and uses a "broker" bot between the website and a Jabber server (I use ejabberd). It's in beta right now mainly because it hasn't been extensively tested outside my company.

Note: while the README specifically mentions the Pyramid web framework, the core system would work just as well with a Django, TurboGears, or command line system. It's just that I only package example code showing how to integrate it with Pyramid.

Seshat is being actively developed. If you have any feature requests, let me know. :-)

Upvotes: 2

Mike M.
Mike M.

Reputation: 563

I'm not sure you need to use MUCs to implement this. Your bot could maintain its own pubsub node which it is subscribed to. When a new user begins to type it could send a notification to the pubsub node, which the bot would then see. From there, the bot could notify a support person via XMPP, thus eliminating the need to long poll the database table. Then the support person could start a standard one to one chat session with the end user. In addition, their presence could be set to 'na' in order to show that they are in a session with a user.

Upvotes: 1

Related Questions