Web Sockets in Google Chrome and Proposed Standard for HTML5

Google is pushing Web Sockets into Chrome based on the Web Socket standards being developed at all major engineering standards groups.  Web Socket is an interesting direction but it is great to couple that with O3G or WebGL for some multiplayer 3d game development with just a browser.

Starting in the Google Chrome developer channel release 4.0.249.0, Web Sockets are available and enabled by default. Web Sockets are “TCP for the Web,” a next-generation bidirectional communication technology for web applications being standardized in part of Web Applications 1.0. We’ve implemented this feature as described in our design docs for WebKit and Chromium.

Sample Code

if ("WebSocket" in window) {
  var ws = new WebSocket("ws://example.com/service");
  ws.onopen = function() {
    // Web Socket is connected. You can send data by send() method.
    ws.send("message to send"); ....
  };
  ws.onmessage = function (evt) { var received_msg = evt.data; ... };
  ws.onclose = function() { // websocket is closed. };
} else {
  // the browser doesn't support WebSocket.
}

Socket Advantage

Flash has long been the answer for sockets for web applications and once sockets were added to Flash it instantly became a better interactive and gaming platform for multi-user applications and multiplayer games. They started with XmlSocket then recently added Socket for raw binary data in as3.  Silverlight and Java also have this feature but having this in script is pretty significant because many applications could really use a browser supported bi-directional communication link.

What is Missing

The biggie missing from Flash, Silverlight, etc and Web Sockets is UDP and preferably RUDP or Reliable UDP which allows UDP datagrams to be sent back and forth either verified delivery or broadcast. Unity does support UDP.  The best socket layers are reliable UDP based because mixing TCP and UDP can lead to queuing and not all messages are critical so having just UDP isn’t enough, having TCP is too much.  Reliable UDP is the way to go but so far no web layers are doing it well except Unity on that one (you still have to make your own RUDP implementation – libraries like Raknet or enet in C/C++ give you this but you can’t use that in Unity client only on the server). (Edit: Flash does have RTMFP which is based on UDP and uses FMS for nat for p2p but it is still not a true low level UDP socket just yet as it supports more features. A low-level UDP socket would also be nice in flash.)

Web Communication Evolving

I am a big Flash fan and have been developing it since 1999 among other platforms, I have recently watched other technologies nearly match the features and some go beyond it.  The interesting thing about Web Sockets is that it does go after a core feature of flash; Canvas and WebGL or O3D also do. Flash still has the webcam, mic, sound mixers/tranform, and for now sockets which put it at an advantage in gaming and interactive. Flash used to  be the sole greatest video player but Silverlight is doing a pretty good job of that as well so that is still an advantage but others are entering including possibly browser support in html5. I still think it is the best video but they would need to keep innovating.

Another interesting point about this is XMLHttpRequest objects.  Originally “AJAX” was created by Microsoft for IE, pushing new features and innovating back when IE was a good browser and ahead in IE4. Mozilla and others adopted this feature (as well as editable text areas for html) because they were great features for web applications to evolve to.  now Google is pushing with Chrome and Web Sockets is the next step that should be in web browsers even if it is only TCP based for now.  This will add great capabilities and will probably be preferred over AJAX/XMLHttpRequest for really interactive and real-time tools/games should it take hold.  Ian Hickson is running the table on the standards with this effort and it is a good one to get behind.

Tags: , , , , , , , , ,

  • http://www.satya-weblog.com Satya Prakash

    I like the simplicity of explanation.

  • Matthew Kaufman

    Flash has reliable and partially reliable UDP in the form of RTMFP, which provides real-time capabilities far beyond what web sockets can do (prioritized media delivery, partial reliability, security, address mobility, etc.)… And instead if just client-server, RTMFP also includes NAT traversal for peer-to-peer media.

    • http://drawlabs.com drawcode

      Hey Matthew,

      Yes I am excited about RTMFP as it is the first foray into UDP for Flash. Adobe Stratus was released about a year ago: http://drawlogic.com/2008/12/12/adobe-stratus-c… but it is still limited to peer to peer and server is mainly a FMS feature, not really available to hook to other systems just yet. It is a good start but the flash player should be able to make a connection (pseudo connection) over UDP to non RTMFP endpoints just like Socket can connect to any TCP endpoint. I am not as concerned with P2P, that will be nice but really we want the ability to sent datagrams over UDP from the flash player.

      • matthewkaufman

        AIR has what you want. Security prevents the same in the browser (how would pure UDP socket policy files work?)

        • http://drawlabs.com drawcode

          Hey Matthew,

          Sure AIR 2 has it but so does all other desktop development, it is only in labs currently as well in the AIR2 beta: http://www.insideria.com/2009/11/udp-socket-con

          We would simply just need that ported to flash web player much like the current Socket class only it would be the Datagram or UDPSocket. Policy files would be able to be the same. Again I am not concerned with P2p much where policies might be much different but the same client server model as the Socket class and like Stratus but without the RTMFP extra layers. It will do wonders for MMOs in Flash and real-time data it will stay ahead of web technologies that currently exist and planned ones like Web Sockets having UDP.

  • http://drawlabs.com drawcode

    Hey Matthew,

    Sure AIR 2 has it but so does all other desktop development, it is only in labs currently as well in the AIR2 beta: http://www.insideria.com/2009/11/udp-socket-con

    We would simply just need that ported to flash web player much like the current Socket class only it would be the Datagram or UDPSocket. Policy files would be able to be the same. Again I am not concerned with P2p much where policies might be much different but the same client server model as the Socket class and like Stratus but without the RTMFP extra layers (for nat traversal and punch through . It will do wonders for MMOs in Flash and real-time data it will stay ahead of web technologies that currently exist and planned ones like Web Sockets having UDP.

  • adijuh

    I am a big Flash fan too
    .

  • adijuh

    I like it

  • adijuh

    this is my comments adijuh.ru

  • http://labs.jam3.ca/ Mark

    This is really cool, so simple.