sirjay
sirjay

Reputation: 1766

nodejs: where session stores? what is connect-mongo?

I'm creating application, using nodejs (0.8.15), express (>3.0) framework, socket.io and mongodb for register users.

  1. Everyone knows that it is easy to create simple chat using only socket.io, without mongodb (or other). So, in this case where stores the messages? in session?

  2. this second question is VERY IMPORTANT for me

I don't understand: why are MongoStore there for sessions? I read that it is "SessionStorage for connect's session middleware/Connect". As I know session destroys when user go away from site. So why do people store it and use require('connect-mongo') ??

var config = require('../config'),
    express = require('express'),
    MongoStore = require('connect-mongo'),
    server = express.createServer();

server.configure(function() {
    server.use(express.logger());
    server.use(express.methodOverride());
    server.use(express.static(config.staticPath));
    server.use(express.bodyParser());
    server.use(express.cookieParser());
    server.use(express.session({
        store: new MongoStore({
            db: config.db
        }),
        secret: config.salt
    }));
});

code I have just found. it seems it is for express 2.x

  1. what is the difference between connect-mongo and connect-mongodb libs? https://github.com/masylum/connect-mongodb

https://github.com/kcbanner/connect-mongo

  1. do I have to make MongoStore for cookies and sockets?

Upvotes: 3

Views: 4126

Answers (2)

Maxence De Rous
Maxence De Rous

Reputation: 166

1) If you don't want to use a database for your chat, you should store the messages into a simple hash. But keep in mind, if you restart your node application, they will be lost.

2) MongoStore (or RedisStore) allows you to store the Express sessions into MongoDB/Redis instead of using the MemoryStore, which is not designed for a production environment.

(by the way the code you've found is for Express 2.x)

3) The two looks similar. Personnally I use connect-mongo.

4) No. Express handles the session cookies for you, and Socket.IO the sockets. If you want to work with MongoDB, you should try Mongoose http://mongoosejs.com/

Upvotes: 4

freakish
freakish

Reputation: 56557

1) Nowhere? Server receives message, broadcasts it and forgets about it. Who sais anything about storing? Server can be just a proxy.

2) Because sessions don't have to be destroyed once a user leaves the site. For example consider a shop. You go to a shop, you put some things in your basket and then you close the browser. After 3 days you go back and you still see your items in the basket. This can only be done with sessions stored in DB ( at least in a reliable way ).

It really depends on your needs. In my case I hardly ever use DB based sessions. I use in-memory storages like Redis.

3) Who knows? Might be some subtle differences, I can't see any obvious.

4) No. As I said: it depends on your needs. If you want sessions to be active only when user is actually viewing the page, then you can stick with any in-memory storage. Or you might not need sessions at all, in which case you don't have to use it at all.

Upvotes: 8

Related Questions