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

1.7k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

Peers need some way to find one another and setup a media connection. A server is a convenient way to do this. You can setup a direct connection for signaling to exchange SDP messages, but whether this will work depends on whether you know the addresses of endpoints and firewalls are configured correctly. Mobile phones and laptops, for example, will have IP addresses that change frequently, which makes locating them directly difficult. The process of exchanging information to start the peer connection is called signaling.

The media in a peer connection is sent normally between peers, though you can also do things like send media through a TURN server.

The signaling portion of the WebRTC process is very lightweight compared to the media sent over the peer connection. Due to the simplicity of using a server for signaling, servers are frequently used for signaling.