7

Is it possible to allow two clients interact directly without a server?

I am referring to websites, for example is it possible to create a chat between two clients that are on the same website using only javascript on the client-side.

If not, what's the minimum server-side to make a chat work between active clients on a website? (eg: one PHP file and no database) ?

My idea: Storing the conversation would be easily done using localStorage on each client, the problem is how to send some data from client1 to client2 without storing anything (or at most that message) in the database. Also, note that "past" conversations should not visible, so no storage needed for that.

Note that I don't want any nodeJS or websocket solutions, I want something as simple as possible. So, what's the minimum code and files to make a chat between online users?

XCS
  • 27,244
  • 26
  • 101
  • 151
  • 1
    Yes. Provide each participant with the other participant's phone number? ;-) Seriously though +1 from me as I'm interested in the answers. – Sepster Mar 21 '13 at 15:19
  • @JamieHutber I guess the OP is asking if it's possible to set up a listener in JavaScript inside the browser (presumably the server would somehow advise each client of the other client's details, then leave it to the clients). Your comment would suggest this is not possible. – Sepster Mar 21 '13 at 15:20
  • It can't be done because at least one of the clients would each need to open up port 80 to requests - this is something only servers do. And even then, there's no way for javascript to accept incoming requests. – Jodes Mar 21 '13 at 15:23
  • 2
    @Cristy this post looks very relevant, although I think the short answer from this is still "no, for now at least, not without resorting to another technology". http://stackoverflow.com/questions/7022383/how-to-make-a-browser-to-browser-peer-to-peer-connection – Sepster Mar 21 '13 at 15:24
  • Or you can think of it more like: send data from client A to server and forward that data to client B, but without using sockets. – XCS Mar 21 '13 at 15:30
  • I wrote in my question that "If not, what's the minimum server-side to make a chat work between active clients on a website? (eg: one PHP file and no database) ?" – XCS Mar 21 '13 at 15:31
  • 2
    Maybe check out the webRTC project? http://www.webrtc.org/ – Christophe Mar 21 '13 at 15:33
  • @Cristy if nothing else pans out, I'd look at implementing something AJAX-y where each client polls the server every so often for updates from the other client. – Sepster Mar 21 '13 at 15:35
  • @Sepster And how would you know the updates without storing anything server-side? Or can you explain a minimal implementation of that (no database)? – XCS Mar 21 '13 at 15:38
  • @Cristy Would a file on the server's file-system work? [PHP file_put_contents()](http://php.net/manual/en/function.file-put-contents.php) and [PHP file_get_contents()](http://php.net/manual/en/function.file-get-contents.php). But I think I'd be considering josh3736's answer :-) – Sepster Mar 21 '13 at 15:43
  • Well, I only have `PHP` available on my hosting. – XCS Mar 21 '13 at 15:52
  • 1
    @Cristy What about offloading the server-side stuff to a 3rd party eg http://www.mibbit.com/ et al? (I've not used these so can't comment on their usefulness or appropriateness). – Sepster Mar 21 '13 at 15:52

3 Answers3

3

The WebRTC APIs will allow JavaScript to initiate a direct browser-to-browser connection, but a server is still required to serve the page and coordinate session initiation.

The APIs are still rapidly evolving and only available in bleeding-edge browsers, so it's not yet ready for real production use.

However—to be honest—for what you're trying to do, the easiest option is Node and socket.io:

var http=require('http'), express=require('express'), sio = require('socket.io')
    , app=express(), srv = http.createServer(app);

app.use(express.static(__dirname+'/static'));

sio.listen(srv);
srv.listen(80);

...and now you have a working websockets server in 5 lines. Put all your client-side stuff in the static folder and you're good to go.

josh3736
  • 139,160
  • 33
  • 216
  • 263
0

No, It's not possible. If you want a chat box, you have to store the data in the server. And what connects the clients, like display the chat texts and the same things to every client, they come from the server.. So it's not possible like that. Well, even free chat boxes put the data of each sites in their servers.

As for your idea using localStorage, maybe it's possible (But still, using the new WebSocket protocol), but it doesn't work in the time dimension, right? if another user joins, they won't see what has been sent before.

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • Well at least, how would I send a text line from client A to client B without storing any data on the server? – XCS Mar 21 '13 at 15:22
  • I said in my question that I don't want any history, simply let the online users interact. – XCS Mar 21 '13 at 15:27
0

HTML5 has got a new Web Sockets feature

With this the server intervention is almost nullified..The server and client communicate through the new protocols

  • ws - Web Sockets protocol
  • wss - Web Sockets Secure protocol (similar to https)

Live demo

funtime
  • 652
  • 1
  • 5
  • 20
  • I know, but I want some minimal implementation which would work on any server, but mostly with `PHP` on the server. WebSockets is pretty hard to set-up for PHP. – XCS Mar 21 '13 at 15:27
  • Web Sockets still requires a server. It just allows a persistent connection between client and server. It's nothing to do with C2C. – Jodes Mar 21 '13 at 15:28