Real-time Chat channel with Socket.io
Unanswered
! nolikas posted this in #help-forum
I have a working Web Socket local server and I need a way to save my messages, so if I refresh the page, the messages stay. I already have a Postgres database made which is hosted on Supabase and I use it for Auth. I wonder if I should stick with the same Postgres or Redis database would be better for saving a chat log. Also, the Socket.io server is hosted on express application, so I'm not sure how do I connect it to my database there.
7 Replies
I have separate server to handle socket.io.
When connecting to Mongo database I also subscribed to changeStream(not sure what it's called in other databases, but basically listening to changes in db).
And then when change happens I emit the reviewChange event.
When connecting to Mongo database I also subscribed to changeStream(not sure what it's called in other databases, but basically listening to changes in db).
And then when change happens I emit the reviewChange event.
the reviewChange behaviour is set up in one of the components.
So useEffect for connecting to socket.io and defining the reviewChange event which when triggered fetches the updated document and changes the data for all clients who have that component rendered on their browser.
So useEffect for connecting to socket.io and defining the reviewChange event which when triggered fetches the updated document and changes the data for all clients who have that component rendered on their browser.
So you connect to database and subscribe to changes on express server, then when change happens omit previously defined event on nextjs app which then runs whatever logic you need.
By the way, if I remember well I had to setup CORS but can't tell much about it because I simply asked chatGPT to do it for me, here's the screenshot if that can help you
Thanks man â¤ï¸
Should I just switch to MongoDB, as I still don't have anything done on Redis yet
With [this tutorial](https://www.youtube.com/watch?v=CQQc8QyIGl0&ab_channel=PiyushGarg) I managed to get all servers I needed online, tho I got one issue. The messages are saved to redis db like they are supposed to be, but now the question is how do I display fetch last 20 messages from redis and display them in a list, instead of displaying only new messages with the redis SUBSCRIBE thing