1

I got an assignment to do and for that I could use any www technology like HTML, JavaScript, PHP etc. I'm really sorry to say that I haven't studied any of these technologies. Therefore I took few tutorials and skimmed through them searching for answers.

I found solutions for many problems but one problem yet unsolved. It is this:

I want two clients to communicate through a server for this assignment. One send a message, server processes it and forwards it to the next.

None of PHP tutorials showed me anyway of doing this. All of them talked of communication between one client with a server.

Please help. Show me a way to do this. Thanks.

PProudy
  • 11
  • 1
  • Send a message...like an email? – k to the z Apr 11 '11 at 19:44
  • No I was thinking of something like clientA sends its status to the server and server sends it to clientB so clientB knows what clientA is doing right now and could decide what he/she should do. – PProudy Apr 12 '11 at 03:30

2 Answers2

3

Currently, without reverting to cutting-edge (and possibly hacky/unreliable) techniques, your PHP server cannot initiate communications with a page you've already loaded into a web browser. This is a result of the way the HTTP protocol works.

One way to solve this would be polling on the "receiving" end for data. Something like a publish-subscribe pattern.

One way to do this would be:

  1. One client sends data to the server using an HTTP request (XHR aka AJAX) specifying the target for this data (the other client).
  2. The server stores this data in a persistent storage (local file, database, etc).
  3. The second client periodically sends a request to the server asking if there's any new data for it to consume. This can be done using setInterval and XHR in JavaScript.

I would suggest you take a look at:

http://en.wikipedia.org/wiki/Publish/subscribe

And also, for a cutting edge way to do this, check out Socket.IO:

http://socket.io

Lior Cohen
  • 8,985
  • 2
  • 29
  • 27
  • Thanks, thank you very much for your knowledge. Too bad PHP cannot do this. Your link on publisher and subscriber is great too (you learn something everyday!). Ok I'll save data in a file. But is there anyway that the server could notify the client that the data is ready without having the client periodically sending requests? That socket program is good. But I think I'd rather stick with PHP for now. Thanks again. – PProudy Apr 12 '11 at 03:19
  • No, as I've said, the server cannot initiate the communication with the client. This is the way HTTP works. – Lior Cohen Apr 12 '11 at 17:59
0

You might want to Google on "php chat server." Building a chat server is a simple way to get started.

http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-simple-web-based-chat-application/

http://code.jenseng.com/jenChat/

rboarman
  • 8,248
  • 8
  • 57
  • 87
  • Thanks for your links. I always thought I hadn't done my researches enough since I only looked up tutorials. Seems it is right. Anyway the links are great. I learned a lot from them. It's a nice start. I'll start from there. – PProudy Apr 12 '11 at 03:24