0

Hope you are doing well,

As I am creating a multiplayer games, so i need to store all user details from socket, after some time I need to make a group from the Users list.

So I tried to implement https://github.com/socketio/socket.io-redis library and trying to get all connected users but it's not working, I have developed according to this example Example to use socket.io-redis, but it'not work for me and my redis server is working well.

Thanks

Example :

index.js

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
var your_namespace_socket = io.of('/');

app.get('/', function(req, res) {
    res.sendfile('index.html');
});
your_namespace_socket.on('connection', function(socket) {

    console.log(socket.id);
    socket.on('join', function(room) {

        socket.join(room);
        console.log(room);

        //log other socket.io-id's in the room
        your_namespace_socket.adapter.clients([room], (err, clients) => {
            console.log(clients);
        });
    });
});
server.listen(3000, function() {
    console.log('listening on *:3000');
});

index.html

<html>

<head>
    <title>Hello world</title>
</head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('/');
socket.emit('join', 'testingroom');
</script>

<body></body>

</html>

Output

User has joined session in a room with cannot fetch data from redis, as you can see in screenshot

Output of Above Code

1 Answers1

0

I tested your code and it works for me, it might be an error with redis, do you have any error in the callback :

your_namespace_socket.adapter.clients([room], (err, clients) => {
    if(err) console.log(err)
    console.log(clients);
});
LaurentP22
  • 586
  • 5
  • 15