websocket server, storing the data

别说谁变了你拦得住时间么 提交于 2021-02-20 18:46:57

问题


Lets say that i make a websocket server chat (node.js + socket.io). How would i store the chat messages, so that when a "new" user joins the chat, he will be seeing old chat messages, and not just the ones which has been sent while hes in the chat.

Should the data be stored in variables in the server? Something like:

var io = require('socket.io').listen(80);

var saveData = { };

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Would it be a viable soultion for a more complex application, like a game for example?

Thanks


回答1:


If you really need to store chat history for new user I would suggest storing those in RDBMS or NoSQL storage. If you choose NoSQL one of options is Apache CouchDB™ (database that uses JSON for documents, JavaScript for MapReduce queries, and regular HTTP for an API). Here is free online book about it.




回答2:


Have a look at redis (redis.io), an in-memory database (disk persistent). Real fast, and your data will persist across server restarts. It also plays really really well with node.js.



来源:https://stackoverflow.com/questions/11940004/websocket-server-storing-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!