callmekatootie
callmekatootie

Reputation: 11238

How to remember data of the client?

I have some information that is unique to each client. This is not the login information but just some generic data of the client - for example, say the address of the client.

Now, when a client connects with the server for the first time, I will read the address of the client.

Subsequently, each time the client makes requests to the server, I would like to use the address that I read from the first request.

How do I do this? I mean, how do I store some information on the client in the server itself so that with each request, I do not have to read the address again (from the DB) but instead read it directly from say, a global variable or a like a request header or something on those lines... How, do I attach the address to each client so that for future requests from the client, I can directly read the address information from the client's request itself without having to query the DB once more...

Upvotes: 1

Views: 778

Answers (4)

Plato
Plato

Reputation: 11072

edit - this requires Express, didn't notice that your question was not tagged express. if you aren't using express you may want to look at the connect.session example, this is what express uses behind the scenes to populate req.session.


Depending on exactly what you want to do with the data, you might prefer to use req.session to store temporary information until the client closes the window or their cookie times out. Here's an example that keeps track of the address across future requests:

var express = require('express');
var app = express();

app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());      
app.use(express.session({secret:'$tackoverflow-rules'}));

app.get('/', function(req, res){
  if(req.session.address === undefined){
    res.send(200,'<html><body><form action="address" method="post">'
      +'<input type="text" name="address" placeholder="Enter address">'
      +'</form></body></html>');
  } else {
    res.send(200,'<html><body><span>I know that you live at '+req.session.address+'!</span></body></html>');
  };
});

app.post('/address', function(req, res){
  if(req.body.address !== undefined && req.body.address !== ""){
    req.session.address = req.body.address;
    res.redirect('/');
  } else {
    req.session.address = undefined;
    res.redirect('/');
  };
});

require('http').createServer(app).listen(3000);

Upvotes: 1

Hector Correa
Hector Correa

Reputation: 26690

If the data is small you can assign the data to a cookie and that way it will be passed back and forth on each request to the server (Localstorage will allow it to keep it on the client but it will NOT be be passed again to the server on the next request)

You can keep a collection in memory with this data on the server and look it up as @tymeJV indicated but this will only works if you have the data is not too big and if you run in a single server. As soon as the data grows you might run out of memory. Also if the server restarts you'll loose this information. As soon as you add a second server this approach will not work because you'll need to make sure the client connects to the same server (which I guess you could do with sticky sessions)

You might want to look into caching this data with something like Redis or another fast database like MongoDB for this kind of session information. That way you'll have a central store for it that could be optimized for performance so your site does not get bogged down while checking for this information.

Upvotes: 0

tymeJV
tymeJV

Reputation: 104795

You can create a global object with a clients address as the key that stores info:

var client = {}
//on connect
client[clientID] = {address: clientAddress} //so on

//To access
client[clientID].address; //or whatever you need

This is how I store client specific information with socket.io

Upvotes: 0

SRay
SRay

Reputation: 286

You can store the information in cookies or localStorage if the data is being displayed on a webpage. If you are using a jsp or php I would store it in a session. What client are you using?

Upvotes: 0

Related Questions