8

Imagine I want to create a realtime multiplayer game, with HTML5 (client) and node.js (server).

I need to transport data very fast from the server to the clients and vice versa.

In a native application I would use UDP for the most data (player position, ...), because it's way faster than TCP and it's uncritical when it is lost.

In HTML5 I can (only) use WebSockets. WebSockets is on top of TCP and thus not fast enough for a good performance.

I heard about WebRTC, but I don't know whether this could be the solution for this problem.

Has anybody experience with it?

(I know, that WebRTC is still unsupported for the most browser, but that doesn't matter to me.)

appsthatmatter
  • 6,347
  • 3
  • 36
  • 40
  • Are you sure that websockets are too slow for your use case? A tcp socket is held open for the duration of your websocket session so you don't have to worry about the overhead of connection for each message. – simonc Oct 12 '12 at 08:51
  • 1
    TCP: Reliable delivery of messages; all data is acknowledged Delivery of all data is managed, and lost data is retransmitted automatically. UDP: Unreliable, best-effort delivery without acknowledgments So TCP is simply the wrong technique for this use case. – appsthatmatter Oct 12 '12 at 09:33
  • 1
    Try implementing it using WebSockets and measure to see if it really is too slow (needless to say, abstract your transport implementation so that if WebSockets do turn out to be unsatisfactory, you can use something else without scrapping lots of code). Right now you're prematurely optimizing. – ebohlman Oct 12 '12 at 10:02
  • 1
    Firefox Nightlies have begun implementing WebRTC DataChannel: https://hacks.mozilla.org/2012/11/progress-update-on-webrtc-for-firefox-on-desktop/ I wonder if it's possible to use Node.js as one peer in the connection. – Willem Nov 09 '12 at 10:01

2 Answers2

2

In terms of WebRTC, sounds like what you need is DataChannel: see draft protocol and HTML5 Rocks article (disclaimer: I wrote it!)

DataChannel is a work in progress, not yet implemented by any browser.

As for other WebRTC components, MediaStream (getUserMedia) is supported by Chrome, Firefox Nightlies and Opera; RTCPeerConnection is in Chrome stable, behind a flag (flagless in forthcoming versions), and promised for Firefox 18 in Q1 2013.

EDIT: RTCDataChannel has now been implemented on Firefox and Chrome.

Chrome 'single page' demo: simpl.info/dc, Firefox demo.

Community
  • 1
  • 1
Sam Dutton
  • 14,775
  • 6
  • 54
  • 64
  • Regarding the latest announcements made, I see MediaStream and PeerConnection are moving forward fast. How is going DataChannel? Also your aticle mentions DataChannel for browser-to-browser communication. It works as well with browser-to-server, right? – Cystack Feb 22 '13 at 17:03
  • 1
    Thanks for pointing this out -- haven't looked at this for a while: added EDIT. On a server, you can use the WebRTC C++ APIs. – Sam Dutton Feb 26 '13 at 17:51
1

RTCDataChannel provides session-based / reliable as well as connectionless / unreliable transport, analogous to TCP and UDP in a native client, respectively. More information here. As of 2013, this is a viable technology, albeit only in later Chrome and Firefox builds.

According to html5rocks.com, it is also now possible to use binary types for transfer. So you should have all the capabilities you would have with an efficient, native UDP client. However, I am as yet uncertain whether binary transfer has made it's way from the webrtc repository, where it has been fixed, all the way into Chrome, or whether it's still only available in Chrome Canary at this stage.

Community
  • 1
  • 1
Engineer
  • 8,529
  • 7
  • 65
  • 105