Two browsers talking to each other via a webserver on the internet?
January 8, 2018 11:57 AM   Subscribe

Is there a JS/PHP library for a situation where two clients can talk to each other via a web server?

The players: Client C1, Client C2 and Server S.

Clients C1 and C2 are HTML5/JS/CSS pages that run in a web browser.

Server S is a webserver on the internet running PHP/MySQL/Apache.

I'm looking to send information between C1 and C2 via S using Ajax/long polling.

Apologies for the vagueness but I don't even know where to begin and thought maybe one of the web guru types can point me in a direction, maybe something that passes XML or or JSON requests between the clients.
posted by ostranenie to Computers & Internet (8 answers total)
 
Response by poster: I'm thinking something like Tango ("The first browser-to-browser communication framework") but I can't find any more information on it.

It doesn't need to be realtime and I don't want to use WebRTC or sockets or node.js or any of that. Just two browsers requesting updates and pushing data to the webserver.
posted by ostranenie at 12:03 PM on January 8, 2018


You describe this two different ways, so--do you really want them talking to each other, or do you want them talking to a server that relays stuff? For the latter, there are about a million various demo chat applications using various methods involving websockets?
posted by Sequence at 12:09 PM on January 8, 2018


Response by poster: I want them talking to each other through the server (they're both behind NAT or something). I don't want to use websockets or WebRTC - just HTTP.
posted by ostranenie at 12:13 PM on January 8, 2018


It looks like the Ratchet tutorial is a chat app of that sort, but I don't know PHP myself so I can't say specifically if this is a great tool or whatever, but it seems like a good starting point.
posted by Sequence at 12:16 PM on January 8, 2018


Oh, sorry, I somehow missed the thing about not using websockets. Is there a particular reason why not? This is what it's for.
posted by Sequence at 12:16 PM on January 8, 2018


Response by poster: I don't want to use Websockets because I don't need full-duplex, real-time communications and I need it to work with a basic web server that has shared hosting.
posted by ostranenie at 12:20 PM on January 8, 2018


Best answer: You could do this with a simple RESTful web service that uses a database to store the messages (many shared hosting accounts include MySQL/MariaDB built-in).

You would interact with the RESTful web service using standard AJAX techniques. C1 would submit a POST request to the web service to save a message into the database. Periodically, C2 would make a GET request to the web service to check for new messages. If there is a new message C2 would then make another request to fetch the message body.

The "check for new messages" service would either need to include logic to make sure old messages are not delivered to C2 more than once, or C2 would need to make a third call to delete a message after receiving it.

Here's an example of RESTful web services in PHP. Disclaimer: I haven't done much with PHP recently, so I can't say if this particular example represents current best practices for REST in PHP.
http://phppot.com/php/php-restful-web-service/
posted by duoshao at 12:35 PM on January 8, 2018


Best answer: AJAX Chat open source project

Roll your own

Roll your own too
posted by Candleman at 12:38 PM on January 8, 2018


« Older Busking sound system questions   |   Grab list of Amazon Prime video URLs? Newer »
This thread is closed to new comments.