I’m trying to open a socket -> PHP Page to being in data from, but I’m unsure on how to open it:
var socket = new WebSocket("ws://myurl.com/sockets/socket.php");
if(socket.onopen)
alert("Open");
else
alert("Not Open");
I have the latest version of jQuery linked in my index (above my link to this script) and there’s nothing in my PHP file – it’s just to see if I can open the connection. Am I missing something or doing something wrong?
WebSockets requires protocol support on the server; specifically, support for a HTTP Upgrade handshake with WebSockets security headers. Simply pointing a WebSocket at a PHP script will not work because PHP scripts speak HTTP, and WebSockets are not HTTP – it’s an entirely different protocol (albeit one based on HTTP).
If you want to use WebSockets in PHP, you’ll need to use a specialized library that implements the WebSockets protocol (I’ve used none of these; they’re just the first Google results).
Of course, Apache and PHP aren’t really the best tool for the realtime job. I’d use Node (and the excellent socket.io package), which is designed to handle a large number of concurrent, persistent connections.