Open hthetiot opened 1 year ago
I've tried to use socket.io-redis as an adapter with easyrtc example server, but it did nothing. Before I detail that attempt, I will describe a successful simple test where I used the socket.io redis adapter to enable the socket.io sample chat app to run multi-instance.
Successful modification of the socket.io chat-example to support multi-instances
It works, and it's as simple as that!
Unsuccessful attempt with open-easyrtc server example
I have modified server_example server.js to use the Redis adapter for its socket.io server. I've added some logging and enabled setting the port param via environment for easier testing. Here is my modified server: https://gist.github.com/krneticz/d78b7dabcfc13b82d46dc770cbfbc645 .
Like the chat test, I run two instances on different ports and open their demos in two browser windows http://localhost:8000/demos/demo_audio_video_simple_hd.html and http://localhost:8001/demos/demo_audio_video_simple_hd.html, but they did not see each other as online - they were not in the same room. Also, during this test in the Redis console, I have not seen any messages exchanged (in the chat test, messages were showing up in the console).
One thing I noticed is that Easyrtc makes socket.io room for each connection, which is definitely not the same as Easyrtc room. Maybe I'm mistaken, but it seems that the Redis socket.io adapter won't do the job here, and code needs to be implemented to "share" Easyrtc rooms between instances, not socket.io rooms.
Thank you @krneticz, I know why instances dont see each other. That because easyrtc nodejs instance in memory object to store states. To solve this I will create an adapter layer and use socket.io room to store room states and also use socket.io to store socket connections states.
Made some progress in local branch using socket.data to store easyrtc Peer state data across servers. I will make PR as a soon as demos basic works.
The data attribute is an arbitrary object that can be used to share information between Socket.IO servers:
// server A
io.on("connection", (socket) => {
socket.data.username = "alice";
});
// server B
const sockets = await io.fetchSockets();
console.log(sockets[0].data.username); // "alice"
In order to be able to implement this changes, i had to refactor https://github.com/open-easyrtc/open-easyrtc/blob/master/lib/easyrtc_public_obj.js and split it in many small Object (Server/Connection/Room/Application/Session).
I hope to be able to push by the end of january, after that we just have to use socket.data as much as possible instead of " e.app[appName].connection[easyrtcid] " (see example https://github.com/open-easyrtc/open-easyrtc/blob/master/lib/easyrtc_public_obj.js#L1656) and move to async instead of sync.
related #108
See https://github.com/socketio/socket.io-redis-adapter