Websocket server, data storage

Suppose I am doing a websocket server chat (node.js + socket.io). How can I store chat messages, so that when a β€œnew” user joins a chat, he will see old chat messages, not just those that were sent while he was in the chat.

Should data be stored in variables on the server? Sort of:

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); }); }); 

Will it be a viable embodiment for a more complex application, for example, for a game?

thanks

+4
source share
2 answers

If you really need to save chat history for a new user, I would suggest storing them in RDBMS or NoSQL repositories. If you choose NoSQL, one of the options is: Apache CouchDB β„’ (a database that uses JSON for documents, JavaScript for MapReduce requests, and plain HTTP for the API). You can download the book for free here.

+2
source

Look at redis ( redis.io ), an in-memory database (persistent disk). Very fast, and your data will be saved on the server restart. It also works great with node.js.

+2
source

All Articles