Search code examples
javascriptserializationwebrtcpeerjs

How do I set metadata in the PeerJs peer.call(id, stream, [options]) function?


I am trying to send two calls in a peer connection and I want to differentiate them through meta_data but I get null when I check meta_data. How can I add meta_data on making calls? This my current code.

let cameracall = peer.call(conn.peer,ourcamera,{
    meta_data:JSON.stringify({ "type":"camera" })
});
let screencall = peer.call(conn.peer,ourscreen,{
meta_data:JSON.stringify({"type":"help"})
}); 

Here is a link to the documentation peercall


Solution

  • for calling a remote peer in normal way without metadata we have:

    peer.call(remotePeerId, ourLocalStream);
    

    for calling a remote peer + attaching some metadata to the call:

    options = {metadata: {"type":"screensharing"}};
    peer.call(remotePeerId, ourLocalStream, options);
    

    on remote peer side, for checking metadata in received call:

     peer.on('call', call => {
        console.log(call.metadata.type);
        call.answer();
        call.on('stream', stream => {
           // somthing to do
        });
     });
    

    please note for other calls that maybe does not have any metadata defined for them, call.metadata.type does not make any sense.