Answer the question
In order to leave comments, you need to log in
Node.js + socket.io and mysql how to organize a connection?
I need to make a chat, for this I decided to use a bunch of node.js + socket.io and mysql.
As a database, the choice fell on mysql, because it is used for the site on which the chat will hang. Both node.js and php scripts of the site will work with the tables that will be used for the chat.
This is where the question comes from.
How to organize a connection to the database in node.js, i.e. connect only once and work with the database through this connection or make a connection for each request from the client, perform tedious actions with the database and break the connection.
Answer the question
In order to leave comments, you need to log in
It is necessary to open a connection pool through mysql.createPool using the https://www.npmjs.org/package/mysql library so that it does not happen that one is busy for a long time with a request, and the other has come asynchronously and wants to be processed while the previous one is still did not return. On the connection break event, you need to hang its resumption. For the case with one connection like this (for a pool, this must be hung on each connection:
var mysql = require('mysql');
connectMySql();
function connectMySql() {
var connection = mysql.createConnection(connectionString);
connection.connect(function(err) {
if (err) {
setTimeout(function() {
connectMySql()
}, 3000);
}
});
connection.on('error', function(err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') connectMySql();
});
}
// Обработчик для API по урлу http://127.0.0.1/example/app/examples/mysql/getCities.json
module.exports = function(client, callback) {
aliasNameMy.query('select * from City', function(err, rows, fields) {
callback({ rows:rows, fields:fields });
});
}
https://github.com/S-anasol/sancrypto/blob/master/...
Here is an example
You put node-mysql or just mysql I don't remember exactly and go ahead.npm install node-mysql
You shouldn't keep the connection open for a long time. Rule: open as late as possible, close as early as possible.
Timur Shemsedinov, I have a question about the database connection pool.
For example, in the main application file, I create a global
app.js pool
global.appChat = {};
appChat.pool = mysql.createPool(config.get('mysql'));
module.exports.getUserById = function(userId, callback){
$sql = 'SELECT * users WHERE id = ?';
appChat.pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query($sql, [userId], function(err, rows, fields) {
if (err) throw err;
connection.release();
callback(null, rows.length ? rows[0] : null);
});
});
};
module.exports.getUsers = function(callback){
$sql = 'SELECT * FROM users';
appChat.pool.getConnection(function(err, connection) {
if (err) throw err;
connection.query($sql,function(err, rows, fields) {
if (err) throw err;
connection.release();
callback(null, rows);
});
});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question