S
S
sldo_ru2020-07-15 17:21:34
Redis
sldo_ru, 2020-07-15 17:21:34

How to store messages in Redis?

There is a chat written in Socket.io+Node.js+Redis .

Everything is done as follows: when entering the page, a new socket connection is created and emit events (auth), after that, a sub redis is created on the server with an identifier by the user ID and a new socket room is created. When sending a message, it is passed to the channel - the user id and the message. Sub.on acts as a message receiver and sends to the socket channel. Due to the fact that pub/sub does not know how to store message history, the question arose: how to store messages on redis, but use pub/sub (because it allows you to easily scale).

sub.on('message', (channel, message) => {
    io.to(channel).emit('message', message)
})

io.on('connection', socket => {
    let uID = null;
    socket.on('auth', userID => {
        uID = userID
        sub.subscribe(userID)
        socket.join(userID)
    })

    socket.on('message', data => {
        pub.publish(data.userID, data.msg)
    })
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2020-07-15
@sldo_ru

With RPOPLPUSH
https://big-elephants.com/2013-09/building-a-messa...
https://stackoverflow.com/questions/6192177/redis-...
But if you need reliable delivery, then you need think about replacing with another MQ.

I
Ivan Shumov, 2020-07-15
@inoise

The solution is not to store history in Redis. Ethylene database, and pumped cache. As a queue, I would not recommend it either - it tends to swallow messages

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question