Do i need to connect to database every HTTP request in NodeJS? - mysql

I am using LocomotiveJS(MVC) based on ExpressJS for developing my first simple API.. I am still in learning phase.. I am using mysql as my database..
My question is, do i need to initiate a connection to mysql everytime there's a controller request?
Here's my code :
SongsController.show = function() {
//this.title = 'Locomotive';
console.log("nice imbasss");
var contacts = SongsModel.foo("GOOD");
var dbConnection = DBUtilities.connectMysql();
var contactsArr = [];
dbConnection.query('select * from contacts', function(err, rows, fields) {
//console.log(err);
console.log(rows);
//console.log(fields);
//contactsArr = rows;
});
DBUtilities.endMysql(dbConnection);
};
As you can notice, everytime songs/show is called, connectMysql() is called.. Am i doing right?

You should connect MySQL everytime when you fire a query to MySQL and should close your connection after that because mysql injection may occur by third party.

Related

problem with using mysql with nodejs server with transaction query

So essentially I am using a transaction in my nodejs server with mysql to pull the first row and then delete that row from the table.
the code for this looks like:
app.get('/', (req, res) => {
connection.query("START TRANSACTION; SELECT * FROM data.train_data LIMIT 1; DELETE FROM data.train_data LIMIT 1; COMMIT;",(err,rows) => {
if(err){
console.log('Error selecting tweets from Database...');
return;
}
console.log('Successfully recieved tweets from database');
res.render('index', {
title: 'Tweet Labeler',
data: rows[1]
});
});
The problem is that even though the query is working correctly, when I reroute to the page in my client side javascript like this:
function relClick(){
var xhr = new XMLHttpRequest();
var newData = {'tweet':data[0].tweet,'relevant':1};
var jsonData = JSON.stringify(newData);
xhr.open("POST","/",true);
xhr.setRequestHeader('Content-type','application/json');
xhr.send(jsonData);
console.log("Clicked Relevant");
xhr = new XMLHttpRequest();
xhr.open("GET","/",true);
xhr.send();
nextItem();
}
I essentially want to repeat this process and grab the new top row from the table but by using print statements I have deduced that it is somehow not realizing that the row has been deleted even though when I use mysql workbench I can see the row has been deleted.
As you can see with this screenshot it continues to grab the row with the id 24 even though it has been deleted?
I am really confused on why this is. Hopefully someone with more experience can assist me because I am very new to this. Thanks in advance!

How can I keep my connection alive for multiple queries using JDBC and Google-App-Script?

I saw this post but it didn't really helped me solving my problem.
I use a HTML dialog box opened with an add-on on google spreadsheets and I'm using JBDC.
I load from multiple queries some datas from my MYSQL database, I also have a search bar to search for datas in the DB and in the future I would like my HTML to automatically show various database values depending on option choosen in my HTMLpage. Basically that's a lot of queries for only one App so I guess there should be one connection object to use.
I've tried multiple things that I can show you in pseudo-code.
open a new connection every time
so here would be my GS file
function firstFunc()
{
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
//do my thing
return (datas);
}
function secondFunc()
{
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
//do my thing
return (datas);
}
function thirdFunc()
{
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
//do my thing
return (datas);
}
and then my HTML
<script>
var onSuccessFirst = function (data){
//update my HTML with data
}
var onSuccessSecond = function (data){
//update my HTML with data
}
var onSuccessThird = function (data){
//update my HTML with data
}
google.script.run.withSuccessHandler(onSuccessFirst).firstFunc();
google.script.run.withSuccessHandler(onSuccessSecond).secondFunc();
google.script.run.withSuccessHandler(onSuccessThird).thirdFunc();
</script>
But as I'm using a free database provider for developping the third connection returns me an error telling me to verify password or username because it failed to connect to database.
try passing the connection from the server to the client then back to the server:
GS file
function getConnection()
{
return (Jdbc.getConnection(dbUrl, user, userPwd););
}
function firstFunc(conn)
{
conn...
//do my thing
return (datas);
}
function secondFunc(conn)
{
conn...
//do my thing
return (datas);
}
function thirdFunc(conn)
{
conn...
//do my thing
return (datas);
}
and then my HTML
<script>
var onSuccessFirst = function (data){
//update my HTML with data
}
var onSuccessSecond = function (data){
//update my HTML with data
}
var onSuccessThird = function (data){
//update my HTML with data
}
var onSuccessConnection = function(conn)
{
google.script.run.withSuccessHandler(onSuccessFirst).firstFunc(conn);
google.script.run.withSuccessHandler(onSuccessSecond).secondFunc(conn);
google.script.run.withSuccessHandler(onSuccessThird).thirdFunc(conn);
}
google.script.run.withSuccessHandler(onSuccessConnection).getConnection();
</script>
But here conn is null.
I also have a lot of queries sent when my input (search bar) is onchange and I use first method it works except it doesn't allow quick typing as it increases connection requests on each character typed.
What can I do?
Maybe Try chaining with your first approach:
<script>
var onSuccessThird = function (data){
//update my HTML with data
}
var onSuccessSecond = function (data){
//update my HTML with data
google.script.run.withSuccessHandler(onSuccessThird).thirdFunc();
}
var onSuccessFirst = function (data){
//update my HTML with data
google.script.run.withSuccessHandler(onSuccessSecond).secondFunc();
}
google.script.run.withSuccessHandler(onSuccessFirst).firstFunc();
</script>
Notes:
google.script.run is a async function. All three runs will be called one by one without waiting for the the previous run to finish, which means almost 3 Jdbc connections will be opened more or less at the same time in the first approach.
A single script.run call will close the connection. So in your second approach, the conn will be null when the first run is over or before that.
JDBC connections close automatically when a script finishes executing. (Keep in mind that a single google.script.run call counts as a complete execution, even if the HTML service page that made the call remains open.)
References:
Closing connections
If you are able to use connection pooling,
else you can keep the connection alive for one complete set of transactions by checking null and opening / closing the connection only once for a full set of request response.

How to end a mysql connection and not let it hang in NodeJS?

I am trying to end my mysql connection in my node program. I can successfully do that using something like connection.end(). However, when I do that it doesn't seem to run the queries before that. If I leave the end statement out my insert queries before this seem to run, however the program then hangs and it just never ends.
Here is my code
var mysql = require('mysql');
var connection = mysql.createConnection({'...'});
connection.connect(function(err){
});
for(var a=0; a<ticker.length;a++){
if(result[i].tweetText.indexOf(ticker[a]) > -1){
for(var j=0; j<positive.length;j++){
if((result[i].tweetText.indexOf(positive[j]) > -1) && check > -1){
console.log(result[i].tweetText + "\n")
plus++;
console.log(ticker[a] + "plus"+plus +"\n")
check = -1;
connection.query("insert into....", function(err, result) {
});
}
}
}
}
connection.end();
Having the end successfully terminates connection, but then all my insert statements do not run; when I remove the end connection statement, my insert statement runs but the program hangs.
You can do what I did and use a mysql pool. Store your connection settings in an object, create the pool using your config variable, and then pass around the con however you want, and then just grab the connection, query your db, and it takes care of everything for you. No hangups.
var db_config = {
user:"",
password:"",
database:"",
port:80
};
var con = mysql.createPool(db_config);
And then use the getConnection function to open and release the connection.
con.getConnection(function(err){
con.query("SELECT * FROM tableName",function(err,result) {
});
});
You can read more about pool connections via the github mysql source
According with documentation when you use conn.query, the connection automatically is open, so you don't need
connection.connect(function(err){
});
Answering your question, the problem is because when you insert into db, node doesn't wait for and continues with the loop. You can push the data into an array and after the loop you can insert bulk data. like this:
How do I do a bulk insert in mySQL using node.js
var bulk = data; //
connection.query("insert into table (columns) values ?", bulk, function(err, result) {
connection.destroy();
});

multiple websockets connections on the same client - server cannot handle

Let me clarify, this is kind of complicated.
I'm implementing a form to insert data in the database.
I have two websockets connections in the same client side, connecting on the same nodejs server.
One connection is triggered after the user inserts a name on the "name" textfield of the form. Sends the data to the server, server checks the database if the name already exists and responces back "This already exists. Mayde you are inserting something that is already there".
The other connection is triggered if all the fields of the form are not blank and sends the data to server to insert them in the database.
I thought it was a good idea to distinguish on server-side ,the different connections using arrays. If the first element of the array is "name" call the checkName function, or if it is "insert" , call the insertInDB function.
I created two small testing files. They do not work. Connections are open and the client sends the data. I get no errors inte server nor the client side. But server never responces. I dont get the expected numbers, back in the client side. I dont think this is the right anyway. This is complecated, I hope the code helps you.
Is it possible, what I am trying to do? Any hints or alternatives?
Thanks
the code....
server-side
function WebSocketTest1(){
var a=1;
var b=2;
var c = [a,b];
var so = new WebSocket("ws://localhost:1337");
so.onerror=function (evt)
{message.textContent = evt;}
so.onopen = function(){
message.textContent = "opened";
so.send(c);
message.textContent = "sended";
}
so.onmessage = function (evt) {
var received_msg = evt.data;
document.getElementById("message").innerHTML=received_msg;
}
}
function WebSocketTest2(){
var d=3;
var e=4;
var f = [d,e];
var sa = new WebSocket("ws://localhost:1337");
sa.onerror=function (evt)
{message2.textContent = evt;}
sa.onopen = function(){
message2.textContent = "opened";
sa.send(f);
message2.textContent = "sended";
}
sa.onmessage = function (evt) {
var received_msg = evt.data;
document.getElementById("message2").innerHTML=received_msg;
}
}
</script>
</head>
<input type="button" value="one" onClick="WebSocketTest1()"><br/>
<input type="button" value="two" onClick="WebSocketTest2()"><br/>
<body>
<div id="message"></div>
mesage2</br>
<div id="message2"></div>
</body>
</html>
on the server side I am listing the sessions, to communicate only with a specific session, code found here
and the server side (snippets)
var connections = {};
var connectionIDCounter = 0;
var connection = request.accept(null, request.origin);
// Store a reference to the connection using an incrementing ID
connection.id = connectionIDCounter ++;
connections[connection.id] = connection;
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
var ja=message;
if(ja[0]==1)
{ja[1]=7;}
else if(ja[0]==3)
{ja[1]=8;}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
delete connections[connection.id];
});
});
// Send a message to a connection by its connectionID
function sendToConnectionId(connectionID, data) {
var connection = connections[connectionID];
if (connection && connection.connected) {
connection.send(ja[1]);
}
I wonder why you need 2 connections.
Open one connection and send the payload as JSON.
e.g.
var ws = new WebSocket("ws://localhost:1336");
ws.send(JSON.stringify({ command: "checkname", params: "xxxx"; });
ws.send(JSON.stringify({ command: "submit", params: { ... });
At the server side, you just have to parse the payload and determine which command is executed.
I am just wondering why you need websocket. It seems that you need to validate user presence in the application. If user is not present then you need to insert in the database else throw error.
you can go through jquery post and jquery form validation
http://api.jquery.com/jQuery.post/

How to uniquely identify a socket with Node.js

TLDR; How to identify sockets in event based programming model.
I am just starting up with node.js , in the past i have done most of my coding
part in C++ and PHP sockets() so node.js is something extremely new to me.
In c++ to identify a socket we could have done something like writing a main socket say server to listen for new connections and changes, and then handling those connections accordingly.
If you are looking for actual sockets and not socket.io, they do exist.
But as stated, Node.js and Javascript use an event-based programming model, so you create a (TCP) socket, listen on an IP:port (similar to bind), then accept connection events which pass a Javascript object representing the connection.
From this you can get the FD or another identifier, but this object is also a long-lived object that you can store an identifier on if you wish (this is what socket.io does).
var server = net.createServer();
server.on('connection', function(conn) {
conn.id = Math.floor(Math.random() * 1000);
conn.on('data', function(data) {
conn.write('ID: '+conn.id);
});
});
server.listen(3000);
Timothy's approach is good, the only thing to mention - Math.random() may cause id's duplication. So the chance it will generate the same random number is really tiny, but it could happen. So I'd recommend you to use dylang's module - shortid:
var shortid = require('shortid');
var server = net.createServer();
server.on('connection', function(conn) {
conn.id = shortid.generate();
conn.on('data', function(data) {
conn.write('ID: '+conn.id);
});
});
server.listen(3000);
So in that case you can be sure that no id duplications will occur.
in typescript:
import { v4 as uuidv4 } from 'uuid';
import net from 'net';
class Socket extends net.Socket {
id?: string;
}
const server = net.createServer();
server.on('connection', (conn) => {
conn.id = uuidv4();
conn.on('data', (data) => {
console.log(conn.id);
});
});
server.listen(3000);
you need to add id first;
In c++ to identify a socket we could have done something like writing
a main socket say server to listen for new connections and then
handling those connections accordingly.but so far i havent found
anything like that in node.js . (the berkeley socket model) Does it
even exist in node.js .. if not i am going back to my C++ :$
You should go back, because JavaScript is a prototype-based, object-oriented scripting language that is dynamic, weakly typed and has first-class functions. They are both completely different languages and you will have to have a different mindset to write clean JavaScript code.
https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7
Session ID
If you made use of the sessionId property of socket in v0.6, this is now simply .id.
// v0.6.x
var sid = socket.sessionId;
// v0.7.x
var sid = socket.id;
if you found this question by looking for socket.io unique ids that you can use to differentiate between sockets on the client-side (just like i did), then here is a very simple answer:
var id = 0; //initial id value
io.sockets.on('connection', function(socket) {
var my_id = id; //my_id = value for this exact socket connection
id++; //increment global id for further connnections
socket.broadcast.emit("user_connected", "user with id " + my_id + "connected");
}
on every new connection the id is incremented on the serverside. this guarantees a unique id.
I use this method for finding out where a broadcast came from on the clientside and saving data from concurrent sockets.
for example:
server-side
var my_coords = {x : 2, y : -5};
socket.broadcast.emit("user_position", {id: my_id, coord: my_coords});
client-side
user = {};
socketio.on("user_position", function(data) {
if(typeof user[data.id] === "undefined")
user[data.id] = {};
user[data.id]["x"] = data.coord.x;
user[data.id]["y"] = data.coord.y;
});
How to identify a client based on its socket id. Useful for private messaging and other stuff.
Using socket.io v1.4.5
client side:
var socketclientid = "john"; //should be the unique login id
var iosocket = io.connect("http://localhost:5000", {query: "name=john"});
var socketmsg = JSON.stringify({
type: "private messaging",
to: "doe",
message: "whats up!"
});
iosocket.send(socketmsg);
server side:
io.on('connection', function(socket){
var sessionid = socket.id;
var name = socket.handshake.query['name'];
//store both data in json object and put in array or something
socket.on('message', function(msg){
var thesessionid = socket.id;
var name = ???? //do lookup in the user array using the sessionid
console.log("Message receive from: " + name);
var msgobject = JSON.parse(msg);
var msgtype = msgobject.type;
var msgto = msgobject.to;
var themessage = msgobject.message;
//do something with the msg
//john want to send private msg to doe
var doesocketid = ???? //use socket id lookup for msgto in the array
//doe must be online
//send to doe only
if (msgtype == "private messaging")
socket.to(doesocketid).emit('message', 'themessage');
});
mmmm, i don't really get what you're looking for but socket-programming with node.js (and socket.io) is really straight forward. take a look at some examples on the socket.io homepage:
// note, io.listen() will create a http server for you
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
io.sockets.emit('this', { will: 'be received by everyone connected'});
socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
sockets.emit('user disconnected');
});
});
on connecting to the server, every socket get an unique id with which you can identify it later on.
hope this helps!?
cheers