open-easyrtc / open-easyrtc

Open-EasyRTC - EasyRTC Free of Priologic
https://open-easyrtc-server-demo.glitch.me/
BSD 2-Clause "Simplified" License
363 stars 106 forks source link

Implement multi easyrtc server support #99

Open hthetiot opened 1 year ago

hthetiot commented 1 year ago

See https://github.com/socketio/socket.io-redis-adapter

hthetiot commented 1 year ago

Related:

krneticz commented 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

  1. Check out https://github.com/socketio/chat-example and install packages.
  2. Make sure you have Redis running on your machine with default settings (or change settings in index.js accordingly).
  3. Add seven lines of code to index.js or overwrite it with my Gist code https://gist.github.com/krneticz/cb21fdb98ebdb32c63fb37a5351121d5 and install additional libs (npm install @socket.io/redis-adapter redis).
  4. Open one terminal and run the server on the default port (node index.js) and open another terminal and run the server on a different port, e.g. 3001 (PORT=3001 node index.js).
  5. Open http://localhost:3000 in one browser window and http://localhost:3001 in another browser window and start sending chat messages, and you will see that they are in the same room.

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.

hthetiot commented 1 year ago

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.

hthetiot commented 1 year ago

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"

Source: https://socket.io/docs/v4/server-api/

hthetiot commented 1 year ago

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.

hthetiot commented 1 year ago

related #108