Hopefully this helps those running Apache/PHP.
If there are any questions, I'll try to answer them. Before asking
questions, please do a search on google as a lot of the answers can be
found there.
1) Download latest NodeJS (0.3.1): http://nodejs.org/dist/node-v0.3.1.tar.gz
2) Check out the install instructions for AJAXIM here:
http://groups.google.com/group/ajaxim/msg/7e215574c7e9573a
3) Ensure that you can hit http://localhost:8000/dev/ and that AJAXIM
is working before proceeding!
4) Insert into your Apache configuration into the V-Hosts directive:
Proxypass /chat http://localhost:8000
Here is an example:
<VirtualHost *:80>
ServerAdmin admin@[domain.tld]
DocumentRoot /var/www/html/[websitedir]
ServerName [domain.tld]
ServerAlias [domain.tld] *.[domain.tld]
Proxypass /chat http://localhost:8000
ProxyTimeout 310
</VirtualHost>
* Note that [websitedir] is the folder of where your website resides
* [domain.tld] is the website, e.g google.com
* For the proxypass line: localhost and 8000 should reflect where your
NodeJS settings (if changed from the default)
5) Integrate the below into your front controller:
<script src="/js/md5.js" type="text/javascript"></script>
<script src="/js/store.js" type="text/javascript"></script>
<script src="/js/cookies.js" type="text/javascript"></script>
<script src="/js/dateformat.js" type="text/javascript"></script>
<script src="/js/im.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var im = AjaxIM.init({
theme: "/modules/ajaxim/themes/default",
pollServer: "http://www.domain.tld/chat"
});
});
</script>
* Note that domain.tld should reflect the website.
6) Custom htaccess rules
Insert these 2 rules into your htaccess file which should reside in
the basedir of your website
# --- AjaxIM ---
RewriteRule ^ajaxim/getuser$ /directory/getuser.php [L]
RewriteRule ^ajaxim/getfriends$ /directory/getfriends.php [L]
* Note that directory should be a directory like ajaxim that exists in
your basedir and holds the 2 files getuser.php and getfriends.php
* [L] is a flag that means that when Apache processes the rules then
it stops processing the htaccess file.
7) getuser.php
Here is an example script to retrieve the username from the session
id. I have my own custom backend and your backend will be different.
The point is that I generate a JSON string that is outputted for the
consumption of NodeJS.
<?php
$sessionid = $_COOKIE['sessionid'];
$sql = "select username from users where sessionid = '$sessionid';";
$result = $dbconn->fetchRow($sql, PDO::FETCH_ASSOC);
$username = $result[0]['username'];
echo '{"username":"' . $username . '"}';
?>
8) getfriends.php
<?php
$arr = array ('friend1', 'friend2', 'friend3');
echo json_encode($arr);
?>
Same principle as the above except that it outputs:
["friend1","friend2","friend3"]
9) See post from Joshua about Authentication:
http://groups.google.com/group/ajaxim/msg/7ae5ab0b6164926c
10) Custom index.js
* Note that ajaxim/ matches up with the .htaccess ruleset.
// Cookie that stores the session ID
// Will be set as request.sessionID in `authenticate` and `friends`
functions
exports.cookie = 'sessionid';
exports.authenticate = function(request, callback) {
var http = require('http');
var site = http.createClient(80, 'localhost');
var req = site.request('GET', '/ajaxim/getuser', {
host: 'www.domain.tld',
cookie: exports.cookie + '=' + request.sessionID
});
req.end();
req.on('response', function (response) {
var data = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk;
});
//console.log (request.sessionID);
response.on('end', function() {
try {
callback(JSON.parse(data));
} catch(e) {
callback();
}
});
});
};
exports.friends = function(request, data, callback) {
var http = require('http');
var site = http.createClient(80, 'localhost');
req = site.request('GET', '/ajaxim/getfriends', {
host: '
www.domain.tld',
cookie: exports.cookie + '=' + request.sessionID
});
req.end();
req.on('response', function(response) {
var data = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
try {
callback(JSON.parse(data));
} catch(e) {
callback();
}
});
});
};
11) That's. I would really put logic in point 5) that it only shows
up when there is a valid session, i.e the user has authenticated.