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