Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket.io performance problems
    text
    copied!<p>We've set up a live ticker for one of our events last night and we ran into seriouse performance issues. Socket.io wasn't able to serve socket.io.js with more than 400 to 500 clients connected to it. The ticker is really simple, there is just one message every few minutes which is broadcasted to all the clients so I don't think there is much room for improvement in the code. The Hardware of the server is not the best but we were monitoring the processes during the uptime of the ticker and none of them was causing the problems.</p> <p>Do you have an any idea how to solve the problem or at least what else could be the reason for it. It looks like socket.io was just struggling but not due to lack of hardware power.</p> <p>Server Structure</p> <pre class="lang-js prettyprint-override"><code>var io = require('socket.io').listen(443); io.set('log level', 9); //SQL CONNECTION io.sockets.on('connection', function (socket) { var sql_items = 'SELECT * FROM entries ORDER BY tstamp DESC'; db_query(sql_items , function(res_items) { socket.emit('init', res_items); }); socket.on('new_entry', function (data) { //SECURE if(!checkedSocketUsers[socket.id]) return false; var currentTime = new Date(); if(currentTime.getMinutes() &lt; 10); var minutes = currentTime.getMinutes(); if(minutes &lt; 10) minutes = "0" + minutes; var hours = currentTime.getHours(); if(hours &lt; 10) hours = "0" + hours; var tstamp = currentTime.getTime() / 1000; var time = hours + ":" + minutes; sqli = "INSERT INTO entries (uid, tstamp, text, type) VALUES (null, "+tstamp+", '"+data.text+"', '"+data.type+"')"; client.query(sqli, function(err, info) { var br_data = {}; br_data.time = time; br_data.text = data.text; br_data.uid = info.insertId; br_data.type = data.type; socket.broadcast.emit('broadcast_entry', br_data); socket.emit('broadcast_entry', br_data); }); }); socket.on('update_entry', function(data) { //SECURE if(!checkedSocketUsers[socket.id]) return false; sqlu = "UPDATE entries SET text = '"+data.text+"' WHERE uid = "+data.uid; client.query(sqlu, function(err, info) { br_data = data; socket.broadcast.emit('broadcast_update_entry', br_data); }); }); socket.on('remove_entry', function(data) { //SECURE if(!checkedSocketUsers[socket.id]) return false; var uid = data.uid; sqld = "DELETE FROM entries WHERE uid = "+uid; client.query(sqld, function(err, info) { var br_data = {}; br_data.uid = uid; socket.broadcast.emit('broadcast_remove_entry', br_data); socket.emit('broadcast_remove_entry', br_data); }); }); }); </code></pre> <p>Client Structure</p> <pre class="lang-js prettyprint-override"><code>socket = io.connect("http://localhost:443"); socket.on('init', function(data) { //DOM Manipulation }); socket.on('broadcast_entry', function(data) { //DOM Manipulation }); socket.on('broadcast_remove_entry', function(data) { //DOM Manipulation }); socket.on('broadcast_update_entry', function(data) { //DOM Manipulation }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload