A PolyFill for WebSockets

– 02/25/13

IE9 and below require the use of a Flash-based polyfill. I’ve had good luck with web-socket-js.

The main “gotcha” involved with the Flash fallback included in web-socket-js is the Flash socket policy file, which is outlined here. Basically, for security, socket connections in Flash require a policy XML file hosted on port 843 of the same location the websockets server is running. It should return the following:

<cross-domain-policy>
  <allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>

So, for instance, if you’re WebSockets server is running on localhost:1234, you’ll need a policy server running on localhost:843. Here’s a simple Ruby Flash Socket Policy Server (which should only be used for testing locally).

A simple Flash Socket Policy Server

Place the following in a file called flash_socket_policy_server.rb:

require "socket"

puts "Starting policy server..."

webserver = TCPServer.new(nil, 843)
while (session = webserver.accept)
  session.print('<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>')
  session.close
end

Then fire it up like so:

sudo ruby flash_socket_policy_server.rb

Note the sudo command, which is needed because port 843 is so low.