I'm using socket.io with express to create a real-time browser game for fun. Until today I tested site on localhost but I decided to test it on server, and it is so slow.
What I'm doing with socket.io
on client
is simple:
When a user press send button, I'm sending some datas:
socket.emit('details', {
user_id: .. ,
balance : ..,
amount : .. ,
payout : .. ,
chance : .. ,
other : ..
});
On server.js
socket.on('details', function(data) {
users.findById(data.user_id, function (err, docs) {
if(docs.length!=0){
var user_id = docs.local.user_id,
user_name = docs.local.email,
clientSeed = docs.local.client_seed,
serverSeed = docs.local.server_seed,
balance = docs.local.btc_balance,
hash = someHash,
user_id = docs.local.user_id,
user_name = docs.local.email,
id = (new Date).getTime(),
lucky_number = /// CalculateRolledNumber();
chance = data.chance,
amount = data.amount,
payout = data.payout,
other = data.other;
CalculateGAME (amount, payout, hash, ..... , ..... ); // calculating
}
});
In CalculateGAME(...) function I'm saving some datas to MongoDB and returning some of them like this:
if (err) return handleError(err);
SOCKET.broadcast.emit('details_RETURN', { // to everybody
id : id,
amount : amount,
payout : payout,
betProfit : betProfit,
result : result,
_user : user_name,
chance : chance, //
lucky_number : lucky_number, //
seed : seed,
hash : hash
});
SOCKET.emit('details_RETURN___self', { // to me
id : id,
amount : amount,
payout : payout,
betProfit : betProfit,
result : result,
_user : user_name,
chance : chance, //
lucky_number : lucky_number, //
seed : seed,
hash : hash
});
And then get back it on client
:
socket.on('details_RETURN', function(data) {
//AppendDatasToTable()
});
socket.on('details_RETURN___self', function(data) {
//AppendDatasToTable()
});
As a result I'm doing:
server.js
Sha512
of seedHmacSHA512
socket.emit
and socket.broadcast.emit
Everything is good on localhost
but on server, even mouse is freezing? Server details are:
4GB RAM, 60GB SSD, Ubuntu 14.04 (DigitalOcean).
How can I optimize my product? (I'm new to nodejs/socket):
Edit: I changed socket.io requests to Ajax but still same problem, I tested site with 32GB Ram, 320GB SSD server and extremely laggy.
There must be a problem with my coding style.