Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending data from easyrtc server to a specific connected client #340

Open
kevinsuwito opened this issue May 19, 2022 · 4 comments
Open

Sending data from easyrtc server to a specific connected client #340

kevinsuwito opened this issue May 19, 2022 · 4 comments

Comments

@kevinsuwito
Copy link

In the demos and tutorial, we can have the client communicate with each other by using NAF.connection.sendData(clientId, dataType, data), which is an extension to easyrtc.sendData() from client side.

Is there a way for the server (for example, using the demo server file easyrtc-server.js) to do that using easyrtc.sendData() ? There seems to be no documentations that specifically address how to do this.

So far I'm doing this but nothing happened:

easyrtc-server.js

easyrtc.events.emit('testmsg', 'hello', easyrtcid);

index.html (client-side)

NAF.connection.subscribeToDataChannel('testmsg', function(senderId, dataType, data, targetObj) {
  console.log(dataType, data);
});
@vincentfretin
Copy link
Member

Good question, I will need that as well if I want to continue my example #328
I didn't look at it yet, if you find how to do that or someone know, please leave a comment.
There may be some interesting doc at https://github.com/open-easyrtc/open-easyrtc I didn't read that.

@kylebakerio
Copy link
Member

The server is using a socket connection, so you use socket.emit, not easyrtc. I have done this on an older version on an app of mine.

some quick old example cold that might help:

socketServer.on("connection", socket => {
  console.log("user connected", socket.id);

  let curRoom = null;

  socket.on("joinRoom", data => {
    console.log('socket join', data)

    const { room } = data;

    if (!rooms[room]) {
      rooms[room] = {
        from: 'socketio',
        name: room,
        occupants: {},
      };
    }

    const joinedTime = Date.now();
    rooms[room].occupants[socket.id] = joinedTime;
    curRoom = room;

    console.log(`${socket.id} joined room ${room}`);
    socket.join(room);

    socket.emit("connectSuccess", { joinedTime });
    const occupants = rooms[room].occupants;
    socketServer.in(curRoom).emit("occupantsChanged", { occupants });
  });

  socket.on("send", data => {
    console.log('socket send', data)
    socketServer.to(data.to).emit("send", data);
  });

@vincentfretin
Copy link
Member

This code looks similar to server/socketio-server.js that you can use with the socketio adapter.
What I'm interested in is an example of code in server/easyrtc-server.js listening to a new event and emitting a response reusing the socket produced by the open-easyrtc library, so using the open-easyrtc api to reuse the security provided by it (like you can't send a message if you aren't in the room, eventually authenticated with a credential)

@hthetiot
Copy link
Contributor

hthetiot commented Mar 3, 2023

Listen to data type VR:

// arguments: callback, type
easyrtc.setPeerListener((socketId, type, data) => {
    console.log(socketId, type, data);
}, 'vr');

Send data type VR to given user

// arguments: socketId, type, data 
easyrtc.sendData('PSpPpqz5aVJq3Pzz', 'vr', {test: new Date()})

Will send data via P2P if connected to peer, otherwise via server.
cc @vincentfretin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants