Reputation: 5148
I'm using Socket.io for the first time and I try to make a simple game. For this time, I have a mongo database structured like that :
| Sessions | | Users | | Games |
|-----------| |------------| |-----------|
| * _id | | * _id | | * _id |
| * user_id | | * game_id | | * ... |
| * .... | | * ... | | |
I already retrieved user session_id
on socket connection with the .set()
function before socket connexion :
var io = require('socket.io').listen(server);
io.set('authorization', function(handshakeData, accept) {
// Retrieve client session here and put data on handshakeData
});
// IO.Socket connection
io.sockets.on('connection', function(socket) {
// session_id are accessible on socket here
});
But now the question: Each time I have a request from client via socket (with an action for a specific game) I have to :
It's 3 requests here only for retrieving informations for each socket connexion. I can have many socket request each second.
I'm scared about how the mongodb can be overloaded by socket request, and I don't know the best solution.
Thank you all for your help, and i'm sorry for my bad english :/
Upvotes: 0
Views: 169
Reputation: 13089
You can save the data for that user in memory on that socket context. (A major advantage of socket based communication over separate request based.) If you're running more than one node server instance, then this may cause trouble if the same user is updated by two different servers. You may wish to have a single mongodb field that is the "last updated" date for that player, you could index it and query only that field instead of pulling all the data. When you update the player, update that field and subsequent requests will then invalidate the cache. The other option for multi-server communication like this is op-log tailing.
Upvotes: 1