With Safari, you learn the way you learn best. Get unlimited access to videos, live online training,
learning paths, books, tutorials, and more.

Working with WebSockets in PhantomJS

This recipe discusses how to open and work with a WebSocket connection in PhantomJS.

Getting ready

To run this recipe, we will need a script that requests a host that also exposes WebSocket connections.

Note

Although PhantomJS version 1.9 does have WebSocket support, that support is limited to the hixie-76 draft of the protocol. PhantomJS 2.0 is scheduled to include the more modern RFC 6455 version of WebSockets. When writing scripts for PhantomJS that intend to use WebSocket connections, we must ensure that the server supports the hixie-76 version of the protocol.