Ajax Push Engine
From WikiStrycore
http://www.ape-project.org/wiki/index.php/How_to_write_an_application_with_APE
Contents |
Set up
- Set up DNS servers : domain.com, ape.domain.com and *.ape.domain.com must redirect to your server.
- Compile Ape server from git
- Edit ape.conf
- Run ape server
MySQL
- If when you execute the server side script you have the Ape.MySQL is not a constructor this means you compiled APE Server without mysql support try to install mysql header file and run build.sh again (on debian apt-get install libmysqlclient-dev)
- Ape.MySQL need a user and a password, it's not possible to use a user that haven't a password
- Selecting empty filed in MySQL might cause bug, avoid this.
How To
This sample code will show you :
- How to connect your APE module to a MySQL server
- Register a command to allow user to receive data from MySQL server
Server side :
function MySQLConnect(ip, user, password, database) {
var sql = new Ape.MySQL(ip + ":3306", user, password, database);
//onConnect callback
sql.onConnect = function() {
Ape.log('You are now connected to MySQL server');
}
//onError callback
sql.onError = function(errorNo) {
Ape.log('Connection error ' + errorNo +' '+ this.errorString());
}
return sql;
}
//connect to MySQL Server
/**
* /!\ You must specify a user and password, mysql module does not support yet connecting with a user without password. /!\
*/
var sql = MySQLConnect('127.0.0.1', 'toor', 'root', 'ape');
//Set up a pooller to send keep alive request each 2minutes
(function() {
sql.query('SELECT 1', function(res, errorNo) {
if (errorNo == 8) {//Something went wrong, connection has been closed
sql = MySQLConnect('127.0.0.1', 'root', 'toor', 'ape'); //Reconnect to MySQL Server
}
}.bind(this));
}).periodical(1000*60*2);
//Register getInfo command
Ape.registerCmd('getInfo', true, function(params, cmd) {
//Get data from mysql table
sql.query('SELECT age, city FROM users WHERE user = "' + Ape.MySQL.escape(params.user) + '" LIMIT 1', function(res, errorNo) {
if (errorNo) {
Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
return ['101', 'MYSQL_ERROR'];
} else {
//Display to logs data received from mysql
Ape.log('Fetching ' + res.length + ' result(s)');
Ape.log('- Age : ' + res[0].age + '\n- City : ' + res[0].city);
cmd.sendResponse('info', res[0]);//Send first result to client
}
});
})
Copy this code in a file named MySQLDemo.js put it in the scripts directory of APE. Then edit main.ape.js and add :
include('MySQLDemo.js');
Client side :
var client = new APE.Client();
//Load APE client
client.load();
client.addEvent('load', function() {
client.core.start({'name': '' + Date.now() + ''});//Start with a random name
});
client.addEvent('ready', function() {
//send getInfo command
client.core.request.send('getInfo', {'user': 'efyx'});
//onRaw info callback
client.onRaw('info', function(params) {
console.log(params);
});
});
Mysql schema for this demo :
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user` varchar(32) NOT NULL,
`age` int(11) NOT NULL,
`city` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`user`, `age`, `city`) VALUES
('efyx', 24, 'Montpellier'),
('korri', 20, 'Montpellier');
MySQL.query
sql.query("SELECT * FROM table", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else {
Ape.log('Fetching ' + res.length);
for(var i = 0; i < res.length; i++) {
Ape.log(res[i].title);//res[i].<column name>
});
}
});
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else Ape.log('Inserted');
});
Inline push
http://www.ape-project.org/wiki/index.php/Libape-controller
http://github.com/APE-Project/APE_JSF/blob/master/Demos/Controller/test.php
http://www.ifc0nfig.com/using-jquery-with-ape-change-the-background-color-with-php/
http://www.ifc0nfig.com/esenape-send-and-receive-sms-in-real-time-using-ape-jquery-php-and-libape_controller/
http://ape.ifc0nfig.com/Demos/EsenAPE/
Misc
http://www.ifc0nfig.com/diving-into-ape-modules-and-the-jsf-creating-topics-for-channels/