I am trying to connect to a mysql database within node and I want to create a socket (using socket.io) each time a new record has been added, however I am getting the following error.
Error: connect ECONNREFUSED 139.0.0.1:3306 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16)
I am new to socket.io so am trying out code from examples online to understand how it works.
Here is my code
const app = require('http').createServer().listen(8000);
const mysql = require('mysql');
const io = require('socket.io')(app);
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'ExampleLogin'
});
db.connect((err) => {
if(err) console.log(err);
console.log("Connected Successfully To The Database");
});
//db.connect();
console.log('Server running on port 8000');
const prev_id = 0;
io.sockets.on('connection', (socket) => {
socket.emit('greeting', 'Hello');
socket.on('check_podcast', (data) => {
const uid = data['uid'];
const query = "SELECT * FROM podcastTable WHERE id =" +uid;
connection.query(query, (err, rows, fields) => {
if (err) throw err;
if(rows[0].id > prev_id) {
socket.emit('new_podcast', rows[0]);
prev_id = rows[0].id;
}
});
});
db.end();
});
when I run my code I get the following output in the terminal
