About six months ago, I was able to successfully code my own WebSocket server script in PHP. Through that, I was able to set up a WebRTC video chat service on my local host. I was quite happy until I realized that in order to deploy it, I needed a web server that gave me access to sockets.
Unfortunately, no shared web hosting allows for sockets and all web servers that offer sockets are expensive. While not an effective solution on a large scale, for the sake of setting up a demo to show people, I want to change the signalling method over from WebSocket to Ajax so that I can show off the WebRTC video chat service I made.
To that end, I've been trying to code something for the past few days, but have had no success in getting the WebRTC peers to capture each other's video.
At the moment, when one client connects to the script, I am using Ajax to send a request to a PHP script that checks whether there are any other active users in the DB. If not, the script then creates an offer, and places the offer in the DB. After that, the client polls a separate PHP script every second to check for an answer from another client connecting to the script.
After that, I then connect to the script from another client, which queries the same PHP script and DB, which then realizes that an active user (the first connection) has already posted an offer, which the second client acquires and sets for the remote description. The second client then creates an answer, which is placed in the DB.
At this point, the first client (which is polling the DB every second) detects that an answer is present and sets that answer as the remote description for the first client. Unfortunately, even after successfully doing all of this, the other client's video isn't popping up.
So here's where I'm confused and have three (multipart) questions:
1) I thought that after both clients set their local description and then sent that local description to the other client and the other client set that received description as the remote description that the onaddstream event was supposed to fire, thus allowing me to display the remote video. However, this isn't happening. This worked fine before when I used WebSocket, but it's not working at all with pure Ajax. Is there something in particular I'm missing? Has the WebRTC spec changed radically in the past six months? I've tried looking at the WebRTC specs, but I don't see any major changes.
2) After getting frustrated with not getting things to work with Ajax, I went back to my WebSocket version and loaded it on my local host. I haven't changed the code at all since last using it (which worked fine six months ago), but now, when I try to use it, sometimes it works, and sometimes it doesn't. Sometimes I get errors related to not being able to set the local and/or remote descriptions. What's up with this? Have there been changes to the specs that would cause this to happen? Related to this, even though I can't get the remote videos to pop up with the Ajax version, I have been echoing a lot of stuff out to the console, and it seems like with the Ajax version as well, sometimes the local and remote descriptions for both clients are successfully set up, and sometimes errors occurs when trying to set the local/remote descriptions for whatever reason, even though I'm running the exact same script each time without any changes. I'm using the latest version of Chrome, and I'm starting to wonder if there's a bug it in or something.
3) Is the onicecandidate event handler required to establish a connection? My assumption was that peers could establish a connection with simply a valid offer and answer, and that the onicecandidate event was use to provide alternate routes, etc., which could lead to a better connection (but aren't required). Am I wrong? If the onicecandidate info is required, how do you recommend I handle this with Ajax as the signalling method?
I know that's a lot of info and a lot of questions, but any information/insight that anyone can offer would be very much appreciated. I've been banging my head against my desk for the past couple of days trying to figure this out, and nothing is making sense.