2

Trying to write a chat, like on facebook, I wondered if two clients can connect to the same PHP-document, or, if 1 PHP-document could communicate with 2 clients at the same time?
(I just took PHP as an example, I don't mind using another server-side scripting language, although PHP is the only serverside scripting language I can program in).
With communicating I mean receiving input AND send output, via AJAX or something like HTML5 SSE.
Is this possible?
How is this called (for googling)?

Thanks.

11684
  • 7,356
  • 12
  • 48
  • 71
  • using jquery or javascript you can write a script that send an ajax call every few seconds that connects to a php FILE that will query a database table you set up to store conversations...... – Rooster Mar 23 '12 at 15:10
  • One for a server and one for the clients. Look for socket programming in PHP, it's much more stable, because it's a stream and has no overhead of thousands of requests. – dan-lee Mar 23 '12 at 15:11
  • PHP probably isn't the best tool for the job, but yes it's possible in a variety of ways. What you need to decide is protocol. HTTP is stateless meaning once it receives input and sends a response it closes. Now you could store that input and then refresh a page through AJAX but it won't be 100% real-time. – Cfreak Mar 23 '12 at 15:12
  • PHP is the perfect tool for the job, just don't use the HTTP overhead – dan-lee Mar 24 '12 at 03:47

3 Answers3

1

You should look into Websockets. There's a library SocketIO which makes it real easy to do live, "realtime" communication between client and server. The server can push the chat messages to the clients.

You also should consider using Node.JS on the server, which has nice support for SocketIO.

TheFist
  • 623
  • 1
  • 5
  • 6
1

There are hundreds of tutorials on the web that can explain how to use some technologies you're very familiar with to create a chat system (PHP, Javascript and MySQL):

http://www.tutorialized.com/tutorials/PHP/Chat-Systems/1

I recommend reading at least one of them so you can understand where each piece fits in the puzzle.

Happy coding and best of luck.

Ryan Kempt
  • 4,200
  • 6
  • 30
  • 41
0

You could make a site which automatically reloads content with AJAX, and you have to make one PHP file, which shows all messages and one which receives messages from the input form. The data you should save in a MySQL-Database.

Dion
  • 3,145
  • 20
  • 37