Search code examples
node.jsapachelaravelsocket.io

Recommended approach to storing chat messages (node.js)


This is my first time developing a web application that requires chat functionality. There are multiple rooms and there are no restrictions as to how many people can join a room. The part that I can't get my head around is the actual approach to storing the messages. The question is more so in two parts...firstly am I correct to assume that unless all the messages are stored in a database, a newly connected user cannot retain all the previous messages? Secondly is it not recommended to save every message to the database as they are sent ? How else could I keep track if they aren't saved in real time ?

Thanks for any advice, appreciate it


Solution

  • If you want to store the data in memory and persist at a time interval, you can use redis for this. Also if all the data are required to store for future reference, you can implement scheduler like resque to transfer the data from redis to your db and free redis for application state data (i.e. more recent data).