WebRTC Video Chat With Only PHP and JavaScript (No NodeJS or WebSockets)

Not long ago I set about trying to create a video chat (Zoom/Skype/Meetings clone) using only PHP and JavaScript.  Every solution and example I found online required that I have NodeJS running a server, which I didn't want to do so as to not complicate matters on the web server.  After many hours of searching and experimentation, I finally found a solution that works.

I based my solution on the preliminary work done by neilsbaloe, though where he uses plain text files, I use a database like MySQL.  Also, I found that with his solution, it only worked about half the time for me, especially if one of the browsers hit "refresh".

 

How to Download and Use

You can view my GitHub repository here.  A few caveats:  this is technically a Drupal 7 module, but you can look over the php code (in the .module file) and pretty easily convert it to whatever PHP environment you wish to work in.  Also, while I am using jQuery in the javascript, that's totally optional. You can easily use vanilla JS if you like.

This code should also be considered a "demo" and not ready for production.  However, you should hopefully be able to read over it and adapt it to your production needs.

 

How it Works

It works by using AJAX to periodically ask the PHP script if there are any new messages.  Likewise, it uses AJAX to ask the PHP script to insert a new message into the database, to be read by a guest client.

At the moment, the code polls the PHP script every 3 seconds.  Again, in a production environment, you might want to make that quicker, and also STOP polling the server once a connection is successful.

 

I sincerely hope this code helps out anyone else trying to make a web-based video chat (ala Zoom, Meetings, Skype, etc) without needing to use WebSockets or any NodeJS code at all.  There's nothing wrong with those things, of course, I just didn't like the idea of having to leave another process running on my webserver forever, which I might have to periodically restart, update, etc.