Redis + Sockets + How to handle it?

Hello everyone, hope you're having a nice Sunday..

I wonder if anyone can help me understand something about redis.
It's something relative new for me, sorry if I ask something that seems obvious..

While I was reading some docs (not Wappler ones) and some videos, I found that it's possible to do some things that amazes me just thinking about it.

For example:
I found some tutorial about storing "online users" on redis which seems to be more efficient than using MySQL.

Now:

There's a Wappler way to store something on redis and retrieve it on some page?

Is there a logic that I'm missing?
Asked Chatgpt and gave me this:

const express = require('express');
const redis = require('redis');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIo(server);

// Redis config
const redisClient = redis.createClient();

// Socket connection
io.on('connection', (socket) => {
    const userId = socket.handshake.query.userId;

    // Online
    redisClient.set(`user:${userId}:status`, 'online');
    redisClient.set(`user:${userId}:last_seen`, new Date().toISOString());

    // Offline
    socket.on('disconnect', () => {
        redisClient.set(`user:${userId}:status`, 'offline');
        redisClient.set(`user:${userId}:last_seen`, new Date().toISOString());
    });
});

// User status
app.get('/user/:userId/status', (req, res) => {
    const { userId } = req.params;
    redisClient.mget([`user:${userId}:status`, `user:${userId}:last_seen`], (err, replies) => {
        if (err) {
            return res.status(500).json({ error: 'Redis error' });
        }
        const [status, lastSeen] = replies;
        res.json({ status, lastSeen });
    });
});

// Start
server.listen(3000, () => {
    console.log('Listening: http://localhost:3000');
});

Hope I made my self clear and thanks everyone :slight_smile:

There's two community members (@tbvgl and @Roney_Dsilva) with redis extensions. Maybe one of them will help?

1 Like

Thanks Keith!
So there's no Wappler way to achieve this?
Redis is used only for cache here?

Will give it a try..
In the worst scenario, maybe some custom code, thanks a lot my friend!

I believe one of the two extensions will allow you to store/retrieve other data in Redis. As far as I know, Wappler does not provide any actions to interact with Redis; they only use it for user session data.

1 Like