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.)
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.)
Share Improve this question asked Oct 12, 2012 at 8:27 appsthatmatterappsthatmatter 6,4173 gold badges38 silver badges42 bronze badges 4- 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 Commented Oct 12, 2012 at 8: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 Commented Oct 12, 2012 at 9: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 Commented Oct 12, 2012 at 10:02
- 1 Firefox Nightlies have begun implementing WebRTC DataChannel: hacks.mozilla/2012/11/… I wonder if it's possible to use Node.js as one peer in the connection. – Willem Commented Nov 9, 2012 at 10:01
2 Answers
Reset to default 2In 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 ponents, MediaStream (getUserMedia) is supported by Chrome, Firefox Nightlies and Opera; RTCPeerConnection is in Chrome stable, behind a flag (flagless in forthing 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.
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., 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.