Real-time interaction between two clients. Possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Real-time interaction between two clients. Possible?

Post by arukomp »

Hi everyone,

I have a question. Is it possible to make real-time interaction between two clients?
Example image:
Image

So User 1 and User 2 are connected to a server.
What I want to do is to pass specific values from User 1 to Server and pass corrected (or the same) values to User 2 in real-time. The less it takes values to send from User 1 to User 2 through Server, the better it is. There will be several buttons and depending on what data has come to that client, I want to change elements' properties (enable/disable buttons, add text to Text Area or better Div or other similar tag and so on...) in the client.

So what I want to know is if it's possible to do it with very low latency (I know it depends on both users' internet connection) and what languages, technologies should I use. I guess I should get hang of AJAX for interaction with server, but I don't know how to make it listen the server for new messages, data, so it could grab it as soon as it arrives to the server. Also, what language/scripting should I use to change page elements' properties? Javascript or something other?

If anyone has experience with these type of PHP scripts, please tell me any information I would need to make my work more efficient and less buggy. Or just point me to any web page, I have time to read :)

Thanks :)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

JavaScript along with Server Side code (PHP) also known as Ajax.

You pointed out just about everything you need to know to get started.
  • "Real" Time depends on Users Connections (Latency)
  • Client Side Language is needed (Javascript)
  • Listening to see what to update (As far as i know this is not possible to "Listen" with javascript. This would require Java, or maybe Flash) Easiest way would be to ping the server every X seconds.
I would say to look at Json as its pretty easy to send data between php and javascript.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Thanks for the answer :)

As for listening, wouldn't it be possible with AJAX? Or is it supposed to send and receive data on request?

If that won't work - is Java hard to learn? Maybe I could program the whole app in Java, would it be better? Finally, what are the differences between Java and Flash? Which do you prefer?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It would be possible with ajax. Or a form of it, I guess. When I made my chat I had a hidden iframe that polled the server every 1 second. Another thing that people seem to overlook when it comes to this kind of stuff is the use of using php sockets. They stay open and listen for data. Upon receiving data from the socket, it might be a little tricky to update the users view, but I think it's definitely possible.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

scottayy wrote:When I made my chat I had a hidden iframe that polled the server every 1 second.
Much better approach is to have a callback to the same AJAX function on request completion (doesn't matter whether it is successful or not). The trick is to have "smart" server side response. Assume that you've made an HTTP POST request by using your AJAX POST function. Your server side application receives it and there are two possibilities:
1) there is no data to be outputted at that moment;
2) there is such data;

Case 2) is the trivial one - your server side script would just response immediately and close the connection. Then your AJAX function will call itself again (immediately) and the loop starts again.

Case 1) is more interesting. Usually, one would make the server side script not to output any data and close the connection. But it is much better, that we do not close the connection, but cycle through a loop for some period of time (lets say 30 sec.) and if during this period there is still no data to be outputted, close the connection. If there happens such data to become available during this period of time, then we break the loop, output the data and close the connection.

This way we save both bandwidth and CPU usage (client and server side). The bad thing about this technique is that we keep the connection to the server open for a long time.

PS: And finally - it will work in real time, without delays. (That's true if the frequency of data change is acceptable for an average AJAX application)
There are 10 types of people in this world, those who understand binary and those who don't
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

arukomp wrote:Thanks for the answer :)

As for listening, wouldn't it be possible with AJAX? Or is it supposed to send and receive data on request?

If that won't work - is Java hard to learn? Maybe I could program the whole app in Java, would it be better? Finally, what are the differences between Java and Flash? Which do you prefer?
Java is not that hard...but to do such application you need java enabled browser, using applet(deprecated) or java web start technology. Same problem with flash...you need browser enabled. Depends on exactly what you're trying to achieve...how tough/sophisticated this server/client application will be. With php/ajax could be slim solution. And also, is this supposed to be web app?!
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

jmut wrote:
arukomp wrote:Thanks for the answer :)

As for listening, wouldn't it be possible with AJAX? Or is it supposed to send and receive data on request?

If that won't work - is Java hard to learn? Maybe I could program the whole app in Java, would it be better? Finally, what are the differences between Java and Flash? Which do you prefer?
Java is not that hard...but to do such application you need java enabled browser, using applet(deprecated) or java web start technology. Same problem with flash...you need browser enabled. Depends on exactly what you're trying to achieve...how tough/sophisticated this server/client application will be. With php/ajax could be slim solution. And also, is this supposed to be web app?!
well, i could build win app, but I don't want people to download things thinking it's a virus or something. although, win app can have more functionality, so I can create my app more interesting. It might be even less laggy than web app, but web app can be accessed from everywhere, unlike win app.

You have given a good idea about win app, but I don't know
what to choose. Functionality or availability (don't know any synonymous to describe that). Any opinions about this?
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

Hi guys,
There is a way to make JavaScript calls itself at a set of time. You can check that site:http://www.elated.com/articles/javascri ... tinterval/
I have not browsed it but this was the first result is Google.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Well it depend on the audiance...who will use this. is it wide open to public..or some company etc.
To use java /web you will need to digitally sign the applet/application otherwise no one will want to use it...With java web start..you basically have desktop application..you just click a link...and you're good to go. Of course client should have jre installed etc etc.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Well it's going to be open to public. No company, I'm doing it alone.

Now I'm confused how to create my application. Website or Windows App...

If I create a Windows App and I want it to connect to the server, do I have to create a server-side app to manage clients or can it be done with PHP or any other way so I could do it on a shared server? I'm not going to have a dedicated server for this, so if having a shared server for Windows application doesn't work, I'm choosing Web app right away. Personally I'd like to make a Win app because it has more functions and should be a lot less laggy, which is very important to me. Please answer if anyone has experience with that.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

AJAX or an iframes are needed, with iframe method you can simple set script timeout to 0 send a 200 ok header, turn on output buffereing and send new data everytime you have it (padded to the bufer size, prob 4096) - recommend going the ajax route though.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

I believe if you want to create something, you need to understand how it (should) work. Right?

Now I don't understand some things about how Ajax should work
to achieve what I need. Any explanation or sample code about listening for data to update clients' browsers, because I'm lost at the moment...
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

w3schools xmlhttprequest should cover it, failing that a quick google for xmlhttprequest :)
Post Reply