Ryan
Ryan

Reputation: 133

Ajax+XMPP or simple JSON file+ajax

I wrote a very simple AJAX chat app with jQuery. It simply sends your message to a PHP file via AJAX, then writes that message JSON-encoded into a text file for the receiver to pick up. The JSON file is simple, like {"userid":1,"msg":"hello there"}, and its stored under the receiver's user ID. Once the receiver detects a timestamp change on the file, it reads the JSON data out of it.

This works well, and it's very fast. But now I'm considering scaling etc, and I am unsure how filesystem-based chat like this would work long-term. Should I seriously consider an XMPP server and rewrite the Ajax stuff around it, or keep what I have? Thanks in advance.

Upvotes: 0

Views: 1297

Answers (2)

Glenn
Glenn

Reputation: 8032

If scalability is your concern, then I would definitely consider replacing AJAX with XMPP even if your app has no chat capability in it at all. Although XMPP has a lot of complexity in it at the protocol level, Strophe does a great job at abstracting away that complexity. Here is some sample code that does the XMPP equivalent to an AJAX call.

/**
 * handle the response from the search request
 * @param {Element} retStanza contains the iq result packet
 */
com.dynamicalsoftware.opeye.admin.Category.prototype.handleSearchResult = function(retStanza) {
    var a = goog.dom.getElement(com.dynamicalsoftware.opeye.admin.appAreaId);
    goog.dom.removeChildren(a);
    var categories = iqresult.getElementsByTagName('category');
    for (i=0; i<categories.length; i++) {
    new com.dynamicalsoftware.opeye.admin.Category(com.dynamicalsoftware.opeye.admin.GuiMode.SEARCHRESULT, categories[i]).render(parentDivTabContent);
    }
}

/**
 * perform a search operation
 * @private
 */
com.dynamicalsoftware.opeye.admin.Category.prototype.search = function() {
    var request = $iq({to: 'category@' + com.dynamicalsoftware.opeye.admin.domain, type: 'get'}).c('query', {xmlns: 'http://www.dynamicalsoftware.com/opeye/admin/category'});
    var outstanza = request.tree();
    connection.sendIQ(outstanza, this.handleSearchResult.bind(this));
}

Upvotes: 1

Brendan Long
Brendan Long

Reputation: 54312

XMPP may be overkill for what you're trying to do. I'd look at some sort of messaging server. For example, with AMQP, you could use:

With STOMP, you could use:

Apparently the Zend Framework has something called Zend_Queue which might also be nice.

See this question (What is a good message broker for PHP?).

EDIT:

As for why you'd want to do this, XMPP is fairly complicated and large. Something like ActiveMQ + STOMP is much simpler. ActiveMQ is the only one of these I've used so I'll talk about it.

To send a message you'd do something like this (using PHP Stomp):

<?
require_once("Stomp.php");

$connection = new Stomp("tcp://your-activemq-server:61613");
$connection->connect();
$connection->send("/queue/someOtherUser", "Hey buddy, let's talk.");
$connection->disconnect();
?>

Then to pick up messages:

<?
require_once("Stomp.php");

$connection = new Stomp("tcp://your-activemq-server:61613");
$connection->connect();
$connection->subscribe("/queue/myQueue");
while(($message = $connection->readFrame()) != null) {
    echo $message->body;
    $connection->ack($message); // Tell the broker that you handled the message
}
$connection->disconnect();
?>

Of course, in a real situation you probably want to set a header saying who the message is from before sending it but I'm not sure exactly how you send/receive headers in that library.

An even better option might be to hit ActiveMQ directly from JavaScript. I suspect security would be hard to implement though.

Upvotes: 1

Related Questions