302

So I'm looking to build a chat app that will allow video, audio, and text. I spent some time researching into Websockets and WebRTC to decide which to use. Since there are plenty of video and audio apps with WebRTC, this sounds like a reasonable choice, but are there other things I should consider? Feel free to share your thoughts.

Things like:

  • Due to being new WebRTC is available only on some browsers, while WebSockets seems to be in more browsers.

  • Scalability - Websockets uses a server for session and WebRTC seems to be p2p.

  • Multiplexing/multiple chatrooms - Used in Google+ Hangouts, and I'm still viewing demo apps on how to implement.

  • Server - Websockets needs RedisSessionStore or RabbitMQ to scale across multiple machines.

Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
1ManStartup
  • 3,716
  • 4
  • 21
  • 26

8 Answers8

369

WebRTC is designed for high-performance, high quality communication of video, audio and arbitrary data. In other words, for apps exactly like what you describe.

WebRTC apps need a service via which they can exchange network and media metadata, a process known as signaling. However, once signaling has taken place, video/audio/data is streamed directly between clients, avoiding the performance cost of streaming via an intermediary server.

WebSocket on the other hand is designed for bi-directional communication between client and server. It is possible to stream audio and video over WebSocket (see here for example), but the technology and APIs are not inherently designed for efficient, robust streaming in the way that WebRTC is.

As other replies have said, WebSocket can be used for signaling.

I maintain a list of WebRTC resources: strongly recommend you start by looking at the 2013 Google I/O presentation about WebRTC.

Sam Dutton
  • 14,775
  • 6
  • 54
  • 64
  • sam I did a similar question related to video streaming from client to server and if it's possible w webrtc: http://stackoverflow.com/questions/21208698/video-streaming-from-client-to-server-which-alternative-use-websocket-or-webrt I would appreciate your help – user1050817 Jan 18 '14 at 19:29
  • 4
    Thanks for the detailed answer ... any update almost two years later? – Crashalot Aug 10 '15 at 18:52
  • 2
    I recommend taking a look at the resources linked to above — see https://g.co/webrtc. – Sam Dutton Aug 11 '15 at 15:56
  • 7
    Also not that (I believe) WebRTC can be configured to be less strict about packet order and stuff, so it can be much faster is you don't mind some packet loss etc (i.e. having the *latest* data is more important than having *all* the data): http://stackoverflow.com/a/13051771/993683 –  Apr 08 '17 at 08:49
  • 1
    Socket.io a "good choice"? Surely you're joking – Luke Apr 20 '18 at 07:19
  • @iONinja I said that WebSocket could be a good choice. Socket.io seemed a pretty reasonable suggestion at the time, particularly for those starting out with WebRTC. – Sam Dutton Apr 21 '18 at 13:18
  • 1
    I think the keywords here are *at the time*. Socket.io's polling fallback feature is now redundant, so you're left with a wafer-thin library that has easy-to-implement features at a horrific performance cost. Don't get me started :D. – Luke Apr 21 '18 at 22:50
  • Thanks @iONinja — good point. I've updated the answer accordingly. – Sam Dutton Apr 23 '18 at 09:01
  • Thanks! Sorry if I sounded condescending, I just have a particular dislike of Socket.io :P – Luke Apr 23 '18 at 09:13
  • 2
    @SamDutton, Surely the server can double up as a peer and use one end of the RTCDataChannel itself? As such for modern web programming **I don't see any advantage of websocket at all?** since RTCDataChannel is UDP / real time? – Pacerier Nov 08 '19 at 21:00
  • IMHO, it's doable to have audio/video streaming with Websockets – quarks Apr 26 '20 at 16:58
95

Websockets use TCP protocol.

WebRTC is mainly UDP.

Thus main reason of using WebRTC instead of Websocket is latency. With websocket streaming you will have either high latency or choppy playback with low latency. With WebRTC you may achive low-latency and smooth playback which is crucial stuff for VoIP communications.

Just try to test these technology with a network loss, i.e. 2%. You will see high delays in the Websocket stream.

ankitr
  • 5,992
  • 7
  • 47
  • 66
  • 5
    For those interested, this stuff is explained further here: http://stackoverflow.com/a/13051771/993683 –  Apr 08 '17 at 08:50
84

WebSockets:

  • Ratified IETF standard (6455) with support across all modern browsers and even legacy browsers using web-socket-js polyfill.

  • Uses HTTP compatible handshake and default ports making it much easier to use with existing firewall, proxy and web server infrastructure.

  • Much simpler browser API. Basically one constructor with a couple of callbacks.

  • Client/browser to server only.

  • Only supports reliable, in-order transport because it is built On TCP. This means packet drops can delay all subsequent packets.

WebRTC:

  • Just beginning to be supported by Chrome and Firefox. MS has proposed an incompatible variant. The DataChannel component is not yet compatible between Firefox and Chrome.

  • WebRTC is browser to browser in ideal circumstances but even then almost always requires a signaling server to setup the connections. The most common signaling server solutions right now use WebSockets.

  • Transport layer is configurable with application able to choose if connection is in-order and/or reliable.

  • Complex and multilayered browser API. There are JS libs to provide a simpler API but these are young and rapidly changing (just like WebRTC itself).

Eido95
  • 1,313
  • 1
  • 15
  • 29
kanaka
  • 70,845
  • 23
  • 144
  • 140
49

webRTC or websockets? Why not use both.

When building a video/audio/text chat, webRTC is definitely a good choice since it uses peer to peer technology and once the connection is up and running, you do not need to pass the communication via a server (unless using TURN).

When setting up the webRTC communication you have to involve some sort of signaling mechanism. Websockets could be a good choice here, but webRTC is the way to go for the video/audio/text info. Chat rooms is accomplished in the signaling.

But, as you mention, not every browser supports webRTC, so websockets can sometimes be a good fallback for those browsers.

Hagai L
  • 1,593
  • 1
  • 18
  • 40
Mikael Holmgren
  • 2,466
  • 1
  • 18
  • 27
23

Security is one aspect you missed.

With Websockets the data has to go via a central webserver which typically sees all the traffic and can access it.

With WebRTC the data is end-to-end encrypted and does not pass through a server (except sometimes TURN servers are needed, but they have no access to the body of the messages they forward).

Depending on your application this may or may not matter.

If you are sending large amounts of data, the saving in cloud bandwidth costs due to webRTC's P2P architecture may be worth considering too.

Tim Panton
  • 469
  • 3
  • 4
  • 1
    It's a misconception that WebRTC is strictly a peer-to-peer protocol. It's starting to see widespread use in industry as a server-based VOIP alternative. – photicSphere Jan 31 '18 at 00:42
  • Also, when we implement WebSocket as a media flow of WebRTC, it uses SIP and the SIP is a plain text protocol which has been used for VoIP. – M. Rostami Dec 25 '19 at 17:38
14

Comparing websocket and webrtc is unfair.

Websocket is based on top of TCP. Packet's boundary can be detected from header information of a websocket packet unlike tcp.

Typically, webrtc makes use of websocket. The signalling for webrtc is not defined, it is upto the service provider what kind of signalling he wants to use. It may be SIP, HTTP, JSON or any text / binary message.

The signalling messages can be send / received using websocket.

Austin
  • 1,709
  • 20
  • 40
13

Webrtc is a part of peer to peer connection. We all know that before creating peer to peer connection, it requires handshaking process to establish peer to peer connection. And websockets play the role of handshaking process.

Rohit yadav
  • 149
  • 1
  • 3
8

Websocket and WebRTC can be used together, Websocket as a signal channel of WebRTC, and webrtc is a video/audio/text channel, also WebRTC can be in UDP also in TURN relay, TURN relay support TCP HTTP also HTTPS. Many projects use Websocket and WebRTC together.

M. Rostami
  • 999
  • 3
  • 16
  • 27
linkingvision
  • 121
  • 1
  • 1