Socket.io namespace stops working after 10 connections

Multi tool use
Multi tool use


Socket.io namespace stops working after 10 connections



I'm using node.js, ejs, express, mysql and socket.io.
My server looks like this (index.js):


module.exports = function(io) {

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: '',
database: 'chat'
});

router.get('/chat/:channel_name', (req, res, next) => {
var channel_name = req.params.channel_name;
// get the countries from the database so we make sure the users are connecting to the correct channel to chat, else redirect to /chat
pool.getConnection((err, connection) => {
if(err) return console.log(err);
connection.query('SELECT * FROM channel WHERE channel_name = ?', channel_name, (err, rows) => {
if(err) return console.log(err)
if(!rows.length){
return res.redirect('/chat')
}
res.render('channel', {channel_name: rows[0].channel_name})
var channel = io.of(`/chat/${rows[0].channel_name}`);
channel.on('connection', function(socket){
socket.join(rows[0].channel_name)
channel.to(rows[0].channel_name).emit('say hi', 'Hi from ' + rows[0].channel_name + '!');
});

});
});
});

router.get('/chat', function(req, res, next) {
res.render('chat');
});

return router;
}



Client:


<!DOCTYPE html>
<html>
<head>
<title><%= channel_name %></title>
</head>
<body>
<div id="message"></div>
</body>
</html>
<script src="/js/lib/jquery/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>
var socket = io(`/chat/<%= channel_name %>`);

socket.on('say hi', function(data){
$('#message').append(`<div>${data}</div>`)
})
</script>



Basically, when I restart the server, and try, for example: /chat/uk, it shows on the html page: Hi from uk!
However, after restarting the page 10 times, (on the 11th time) the browser keeps loading and it shows GET /chat/uk - - ms - - on the command prompt, why is this happening?
Also, is this the right way to use namespaces? I want to create multiple channels(namespaces) that users will connect and then chat 1 on 1 privately (rooms)


/chat/uk


Hi from uk!


GET /chat/uk - - ms - -




1 Answer
1



This is how the system reacts when you do not write a response and allow the app to hang.


GET /chat/uk - - ms - -




"- -" is blank, meaning there is no response code, time or size. Translated, you are seeing the unknown properties of the response.



Your code is not responding nor generating an error code, and never makes it to these lines:


if(err) return console.log(err) //happens twice


return res.redirect('/chat')


res.render('channel', {channel_name: rows[0].channel_name})



The issue could be on this line:


connection.query('SELECT * FROM channel WHERE channel_name = ?', channel_name, (err, rows) => {



You never close your previous connections to the database. Try adding


connection.release();



at some point in the code, because the available connections to the mysql database are potentially being used up. To check, you can look at directions here
How to get number of unused/used connection in nodejs mysql connection pool ?
or use these commands


pool.config.connectionLimit // passed in max size of the pool
pool._freeConnections.length // number of free connections awaiting use
pool._allConnections.length // number of connections currently created, including ones in use
pool._acquiringConnections.length // number of connections in the process of being acquired



Let me know if this works or if there are follow up questions.





Ah, yes. connection.release(); fixed it :) Thank you!! Also, an extra question, am I on the right track? Should it be done this way or there's another (correct) way to create an app with multiple channels?
– Cedric Hadjian
Jul 1 at 2:27


connection.release();





So for that, I am not positive when I did this before I used redis to manage channels, i would need to look back
– Legman
Jul 1 at 3:04






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Ao3SLMEwQ St,sBkCa elMpjUNaO,K3NbFrvsfhmu K7pBG xhb4QkG4,MgVzhQI,LzE,VDvPf00 35X6CET,m5vrBSI3xbpvdEMkfd38q
laCmf 7ndN5 iOD03 5dv7BW1H40O0PCi CI6M8MuXO9

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask