Works without Redis. But I need to add it to work with multiple hosts, because... I use pm2 on my project. When I enable Redis adapter (commented strings in serversideConnect.ts) => get error:
Create socket host!
Socket connected, ID: iyAnIThaSILp-4zuAAAB
Join room!!!
project_path/node_modules/socket.io/dist/socket.js:535
return this.adapter.socketRooms(this.id) || new Set();
TypeError: this.adapter.socketRooms is not a function
If I enable without io.adapter(redis({host: 'localhost', port: 6379})); I get when console.log('rooms: ', io.sockets.adapter.rooms) => Map { '_L6Jh6SrZaS1nU7ZAAAB' => Set { '_L6Jh6SrZaS1nU7ZAAAB' } }, with redis adapter => {}
// server.ts
import serversideConnectSocket from '../socket/serversideConnect';
const app: Express = express();
const server: http.Server = http.createServer(app);
serversideConnectSocket(server);
// serversideConnect.ts
const sockets = require('socket.io');
// const redis = require('socket.io-redis');
import chatServer from './socketForVChat';
const bindListeners = (io) => {
io.on('connection', socket => chatServer(socket, io));
};
const initializeSocketHost = (server) => {
console.log('Create socket host!');
const io = sockets(server, {
transports: ['websocket', 'polling']
});
// io.adapter(redis({host: 'localhost', port: 6379}));
bindListeners(io);
}
export default initializeSocketHost;
// socketForVChat
export default (socket, io) => {
console.log('Socket connected, ID: ', socket.id);
socket.on(SOCKET_ACTIONS.JOIN_ROOM, data => {
const { rooms: joinedRooms } = socket;
if (Array.from(joinedRooms).includes(roomID)) {
return console.warn('Already connected');
}
const clients = Array.from(io.sockets.adapter.rooms.get(roomID) || []);
console.log('socket clients: ', clients);
console.log('room: ', roomID);
}
}