Skip to content

Using flash as WebSocket fallback

Nik Voss edited this page Jun 10, 2013 · 2 revisions

Golem does not provide any fallbacks for WebSockets, because every major browser does support WebSockets on desktops and every up-to-date mobile. Flash sockets can be used as fallback on the client-side without any change of the server-side except the need to provide a policy file. The excellent and stable web-socket-js library deals with this and provides a WebSocket JavaScript class even if not available by exposing it through a small flash application. To use flash sockets as fallback no code changes are necessary only the JavaScript files of web-socket-js need to be included before golem's and the WEB_SOCKET_SWF_LOCATION needs to be set prior to using WebSockets. So the head of a echoing flash example would look like this:

<script src="flash-websocket/swfobject.js"></script>
<script src="flash-websocket/websocket.js"></script>
<script src="js/golem.js"></script>
<script>
    WEB_SOCKET_SWF_LOCATION = "flash-websocket/websocket.swf";

    var conn = new golem.Connection("127.0.0.1:8080/ws", true);

    conn.on("echo", function(data) {
        console.log("ECHO: "+data);
    });

    conn.on("open", function() {
        conn.emit("echo", { msg: "Hello, flash socket!" });
    });
</script>

If the fallback should be tested, the easiest way to do it is to simply undefine the WebSocket event before other includes:

<script>
    window.WebSocket = undefined;
</script>

If a flash policy file is provided the client can use flash sockets to communicate over WebSockets. The working client and server can be found in the example repository. If you are interested in how you would serve the flash policy in go have a look at the source. On Unix systems the example needs to run with additional permissions, because the default port for flash policies (843) is smaller than 1024 and therefore needs special permissions on Unix systems.

The flash fallback should be considered with care if handshake verification is used with sessions et cetera. For more information see the Cookie support section in the README of web-socket-js.

Clone this wiki locally