chirag lathiya chirag lathiya - 1 year ago 72
MySQL Question

how to get express-session value without any request in node.js

i am making a web service for the ride tracking API for that i am use node.js, MySql and POSTMAN (Crome APP)

in this web service i need to use TCP socket so i used simple 'net' package instead of '' package and i need particular ride data in my socket so i am use 'express-session' package

First i am set session of ride id then then try to run it in socket but express-session's value cannot fetch without any request so i cannot use it in MySql query

here i add some related code of this process.

app.js is as bellow

var trackride = require('./routes/trackride');

var app = express();

var net = require('net');
var sockets = [];

var session = require('express-session');

var svr = net.createServer(function(sock) {

console.log('Connected: ' + sock.remoteAddress + ':' + sock.remotePort);

var query = connection.query('SELECT * FROM tbl_trip WHERE id = ? ',[############# HERE I WANT SESSION RIDE ID #############], function(err, results) {

if (err) throw err

driverlat = results[0].driverlat;
driverLong = results[0].driverlong;
passengerLat = results[0].passengerlat;
passengerLong = results[0].passengerlong;
pathString = results[0].pathString;



app.use('/trackride', trackride);

var svraddr = '';
var svrport = 6969;

svr.listen(svrport, svraddr);
console.log('Server Created at ' + svraddr + ':' + svrport + '\n');

my route file 'trackride.js' to set session is as bellow

var express = require('express');
var router = express.Router();
var url = require('url');'/', function(req,res,next){

var reqObj = req.body;

var rideId = reqObj.rideId;

req.session.cookie.rideId = rideId;


is there any way to use express-session value without request OR is there any other way to solve out my this issue??

please provide me solution or related working programme link.


rsp rsp
Answer Source

You have completely mixed Express, http, TCP sockets, WebSocket etc.

First of all, the concept of express-session doesn't make sense outside of the HTTP request. It tells you what HTTP session a particular HTTP request belongs to. How do you want to know that without any HTTP request taking place? It doesn't make any sense.

Second, you are creating an Express application but you don't make it listen on any port so it is not really having any effect whatsoever.

Third, you set up a bunch of global variables inside of your TCP server handler which would conflict as soon as you have more than one request.

What you probably need here (though it's hard to guess from your question) is WebSocket. You can see my example of WebSocket client and server that I posted on GitHub:

Take a look at how it works and if this is the kind of "socket" that you need.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download