Test your web browser for Web Socket support

Google announced an improved way for a web application to communicate with a server. The new method is called Web Sockets. You can read more info here on Web Sockets.

Here is a quick test to see if your web browser supports Web Sockets. At the time this post was published Google Chrome developer channel release 4.0.249.0 is the only browser to support Web Sockets.

Here is the JavaScript code (or right click and save file as WebSocketTest.js):

function WebSocketTest()
{
  if ("WebSocket" in window)
  {
    // Google example code
    //  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. };
    alert("WebSocket supported here!\r\n\r\nBrowser: " + navigator.appName + " " + navigator.appVersion + "\r\n\r\ntest by jimbergman.net (based on Google sample code)");
  }
  else
  {
    // the browser doesn't support WebSocket
    alert("WebSocket NOT supported here!\r\n\r\nBrowser: " + navigator.appName + " " + navigator.appVersion + "\r\n\r\ntest by jimbergman.net (based on Google sample code)");
  }
}

Sample HTML code (save file as WebSocketTest.html in same folder as .js file above):

<html>
<head>
<title>JimBergman.net - JavaScript: WebSocketTest</title>
<script type="text/javascript" src="WebSocketTest.js"></script>
</head>
<body bgcolor="#FFFFFF">
<a href="javascript:WebSocketTest()">Run WebSocket test</a>
</body>
</html>



UPDATE:

Result of this test on an Windows 7 PC in Google Chrome v4.0.249.0


Result of this test on an Windows 7 PC in Mozilla Firefox v3.5.5


Result of this test on an Windows 7 PC in Microsoft Internet Explorer v8.0.7100.0


Result of this test on an Apple iPhone in OS 3.1.2

  1. View Comments to “Test your web browser for Web Socket support”

  2. Works in WebKit nightly builds as well.

    By kefka_the_cruel on Dec 14, 2009

  3. Thanks for the additional info. That means in the future Web Socket support should be in Safari on the desktop, and the iPhone and Android mobile browsers, as those are all based on WebKit.

    A full list of WebKit-based browsers is on Wikipedia

    By Jim Bergman on Dec 14, 2009

  4. Safari on iPhone OS 4.0 (build 8A230m) claims to support WebSocket.

    By Arun on Apr 16, 2010

  5. That is confirmed with iPhone OS 4 beta 2. I assume once the iPad goes to OS 4 it will support WebSocket as well.

    Thanks for checking that out Arun!

    By Jim Bergman on Apr 26, 2010

  6. I am using Safari 4.0.5 on Snow Leopard and websockets work (tested via using PusherApp.com), but this script reports that it doesn't work… Chrome did report back that it worked. I am looking for some JS that would check if websocket support was enabled.

    By Eric Pugh on May 14, 2010

  7. works in Mac Safari Version 5.0 (6533.16)

    By Paul Leman on Jun 22, 2010

  8. I downloaded iOS 4 on my iPhone 3GS and when I go to this page on it, it says “WebSocket NOT supported here!”.

    By Sam Lalani on Jun 22, 2010

  9. With the final iOS 4.0 release, Apple seems to have removed WebSocket support from Safari.

    By Nobody on Jun 25, 2010

  10. Bzzt. Sigh.

    By John22 on Jun 27, 2010

  11. firefox 3.7 alpha 6 – finally supports websockets and is doing fine – get it here http://nightly.mozilla.org/

    By Pinetree on Jun 28, 2010

  12. You could also type this in your browser's address bar instead of a URL:

    javascript:alert(“WebSockets are ” + (window.WebSocket ? “” : “not “) + “supported”);

    By Chris O'Brien on Jun 29, 2010

  13. Works great! Thanks!

    By Jim Bergman on Jun 29, 2010

  14. To note, just because the browser finds the object doesn't mean it's actually implemented. Chrome 6 and Firefox 4b2 both show false positives. To see if your browser can actually use websockets you might want to check: http://html5demos.com/web-socket

    By Mr. Socket on Jul 2, 2010

  15. The html4demo web-socket test is a nice chat app that run via sockets using ws:// and includes source code. Works in Chrome 5.0.375.99, but not in Firefox 3.6.6 or IE8. Thanks for the link and info!

    By Jim Bergman on Jul 3, 2010

  16. I like this method. It's better to have a concrete test for functionality, and it works on Chrome on Ubuntu.

    By Rsanchez1990 on Jul 22, 2010

  17. I like this method. It’s better to have a concrete test for functionality, and it works on Chrome on Ubuntu.

    By Rsanchez1990 on Jul 22, 2010

  1. 5 Trackback(s)

  2. Dec 9, 2009: Tweets that mention JimBergman.net » Test your web browser for WebSocket support -- Topsy.com
  3. Jan 28, 2010: links for 2010-01-28 « sySolution
  4. Jun 21, 2010: Firefox 4 gets web sockets support • Mozilla Links
  5. Jun 22, 2010: [Brève] Firefox 4.0 supporte les Web Sockets - Websourcing.fr
  6. Jun 30, 2010: Firefox 4 con soporte para Web Sockets « Mozilla Links en español

You must be logged in to post a comment.

blog comments powered by Disqus