Test your web browser for WebSocket support

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

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

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

You can find more information on WebSocket at the Mozilla Developer Network.


UPDATE May 4th, 2012:


Updated to report browser data using navigator.userAgent.
The previous version of this script used browser data from navigator.appName and navigator.appVersion, which are not consistent in different browsers (mainly Firefox).


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

Creative Commons License
Test your web browser for WebSocket support by Jim Bergman is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Permissions beyond the scope of this license may be available at http://jimbergman.net/about.

Tagged with: , , , , ,