Answer the question
In order to leave comments, you need to log in
Handling users in nodejs server?
Good afternoon!
I ask for help in understanding the design of a client-server application.
Server on nodejs, socket IO, MongoDB database is used to connect with the client.
I need to authorize the user, then get the data from the database by ID and then process it on the server.
User data is processed and changed once per second.
for example
var io = require('socket.io').listen(server);
var timer;
io.on('connection', function (socket) {
console.log('подключился пользователь');
socket.on('disconnect', function () {
console.log('пользователь отключился');
});
socket.on('initGame', (id) => { player.GetPlayerData(id,socket);});
});
// работа с данными пользователя
function UpdatePlayerData(data){
setInterval(() => {
//обновление данных
}, 1000);
}
//получение данных с БД
function GetPlayerData(id, socket) {
Player.findOne({ id: id}, function (err, res) {
if (err) throw err;
UpdatePlayerData(data);
});
}
Answer the question
In order to leave comments, you need to log in
The only downside is the timer. It is worth mastering the full event interaction and carrying out all operations in this way. For example, there is a GraphQL protocol and it has a Subscription that works just on a websocket. Other operations in the GraphQL server generate events and they are sent to everyone who subscribes to them.
In a more complex case, only the storage of the user-connection mapping is added and new data is sent to all user connections in a cycle
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question