Jump to content

PHP Chatroom and Live Interaction


GroundVibes

Recommended Posts

Hi,

 

I am currently in the process of conceptualizing an interactive website for users to navigate on.

 

So my first question:

1) Live Interaction

What I mean by this (Lets say the screen is divided in section A and B)

Section A contains: An image along with text

Section B contains: List of users

What happens is that as users enter this page, I want them to be displayed in section B. I can see my new users that are now in the chatroom if I hit F5 (Refresh), but this is not what I want. I want my users to display in section B as soon as they enter to everyone that is already on this page.

 

2) PHP Chatroom

Well, I know there are a couple of chatroom coded in PHP here and there. But what I want is for myself to code it, but as simple as possible. And again, once someone adds a message, I want it to be displayed automatically on the page.

 

 

I guess I could set a timer and once every 5 seconds, the section refreshes, but I am positive there is another way around this. Anyway, if anyone can guide me towards something similar or exact to what I am asking, I would be very grateful.

 

Thank you

 

Andrew

Link to comment
https://forums.phpfreaks.com/topic/44414-php-chatroom-and-live-interaction/
Share on other sites

Alright well I thank you 2 for the reponses on this.

 

So, I picked up a 300 page book on Ajax from the office and started reading in.

 

At the moment I am 100 pages in and its concept itself is flowing smoothly into me!

 

Although, I pretty much understand in Theory how to make it work, but in practice, its a completely different story.

 

Does anybody here have suggestions on where I should start, or a website that provides very low features of Ajax that I can dwell and practice on?

 

I would also like to ask that for a simple webpage that displays new elements as soon as there is a change inside the database, how would I do this? In other words, if person X is watching page A, person Y just modified something inside the database. How does Page A refresh itself to show the result person Y added to person X automatically.

 

Of course, I can and will eventually figure out the logic behind Ajax, but to get this previous example I just mentioned, it seems that I will have to code an eternity of code to get the result I desire and I may be on the wrong track...

 

Anyway, any pointers would help, thank you

 

Andrew

 

for 2) PHP Chatroom, well, you need to write a php socket server... I can help you on that, however, I only know how to write the client side in flash, and not html/php.

Here I will demonstrate a basic one that when receive stuff, simply send it to all users regardless of what the information is.

 

<?php
$host = "127.0.0.1";
$port = 1234;
//don't time out!
set_time_limit(0);
//create a socket
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//bind the socket to a port on the host
socket_bind($mysock, $host, $port);
//set up the socket to listen for maximum 50 users at the same time
socket_listen($mysock, 50);
echo "Waiting for connections...\n";

$clients = array();
$read = array();
$counter = 0;

//now lets enter a loop
while (true)
{
$read[0] = $mysock;
for ($i = 0; $i < $counter; $i++)
{
	if ($myClients[$i]["socket"]  != null)
	{
		$read[$i + 1] = $myClients[$i]["socket"];
	}
}
//this is the key point: we listen for anything that happens to all current sockets.
//the code will stop running until some changes happens to any of the sockets
//don't care about some uninitialized variable; they represent null, which is what we want.
$ready = socket_select($read, $write, $except, $timeout);

//if someone is trying to connect
if (in_array($mysock, $read))
{
	$client = socket_accept($mysock);
	$myClients[$counter] = array();
	$myClients[$counter]["socket"] = $client;
	$counter = $counter + 1;
	echo "Connection received\n";
}
//if user is trying to send data, handle it now
for ($i = 0; $i < $counter; $i++)
{
	$client = $myClients[$i]["socket"];

	if (in_array($client, $read))
	{
		$input = socket_read($client, 1024, 50);
		echo "Received input: ".$input."\n";

		//if user is disconnected
		if ($input === FALSE)
		{
			echo "User gone\n";
			socket_close($client);
			unset($myClients[$i]);
			array_values($myClients);
			$counter = $counter - 1;
		}
		else
		{
			for ($j = 0; $j < $counter; $j++)
			{
				//this writes output to clients
				socket_write($myClients[$j]["socket"], $output, strlen($output));
			}
		}
	}
}
}
?>

That's one way to do it. I never got into AJAX too much, but from what I did read, it sounds like you could just use a refresh via AJAX. Just keep querying the server for changes in the DB in this case. I have this book, which actually has an example of what you're looking for. I haven't built it, and I can't say that I really recommend that book either. Too much code and not much explanation, but if you're smarter than I you may be able to pick up more out of it. I do like that it has a bunch of working examples that you can use for your own applications, something I've found missing in other books.

You know how I learned AJAX?

 

I found a script that did something similar to what I wanted. I used

 

http://blog.jalenack.com/ajax/

 

To base my live chat off of. I know its for a shoutbox and wordpress plug in but you can manipulate it to go with your site.

 

After that I went through the code figured out what it did and than revamped it to my needs.

 

Simple as that =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.