I am trying to connect to Local SQL Server database using Tedious in node js.
function connectToSql() {
sqlConnection.on('connect', function (err) {
// If no error, then good to go...
if (err) {
console.log(err);
} else {
console.log('connected to SQL!');
executeSelectStatement();
}
});
}
When I am invoking this function soon after its declaration, its working fine and prints 'connected to SQL!'. But when I am calling this function "connection" event of SocketIO.Server its not making sql call. My code is as below:
ioServer.on('connection', function (socket: SocketIO.Socket) {
console.log('a user connected');
connectToSql();
});
it only prints 'a user connected'
Any suggestions why is this happening?
It is likely that the SQL connection has already been established before the Socket.io connection takes place. That being the case, the connect
event has probably already happened.
If you're looking to only establish the SQL connection after receiving a Socket.io connection, you probably need to new Connection(config);
at that time while also subscribing to the connect
event.
Do note that you probably do not want to establish an SQL connection for every Socket.io connection as with multiple people utilizing the application, a lot of additional overhead is taking place where it likely does not need to.