Search code examples
javascriptchatwebrtcpeerjspeer-connection

Does PeerJS or WebRTC peer connection require a server or it's only between clients?


I'm working on a project which should allow users to connect with one another (1 on 1) and chat via the website. If I go with the http://peerjs.com/ implementation or another custom solution for WebRTC peer connection, will I need a server to broker the connections? If so, what's the purpose of it? I thought the whole point of WebRTC peer connection was to allow for direct user-to-user connections.


Solution

  • I thought the whole point of WebRTC peer connection was to allow for direct user-to-user connections.

    Direct user to user connection means P2P connection is when two users exchange data directly without any relay server in the middle. But for them to be that way they need to first connect to each other. And for that they need each other IP addresses and other related information.

    Two devices on internet can't know each other addresses unless someway they exchange these infos between them. That's why a Signaling server like SIP,XMPP are used. Peers log into these servers for exchanging these IP information. When Peer A and Peer B are logged into say in a SIP server then when peer A wants to communicate with peer b,

    1) A will send its IP info to the sip server. Sip server will forward this info to B.

    2) Upon receiving the information peer B will send its IP addresses to the SIP server and SIP server will forward it to Peer A.

    3) After they both know each others IP information they can then connect with each other directly without the using the SIP server again.

    This is what P2P connection is. It uses signaling server only for creating connection between peers after that its P2P connection.

    But P2P connection is not always possible even after knowing all the Ip information of each peers. There are some NATs which makes it impossible to create P2P connection. But that is another topic.

    Hope this clears up your confusion.