Skip to content
Daniel Bulant edited this page Feb 4, 2019 · 2 revisions

This tutorial will guide you in setting up a basic chat server.

At a minimum, you need shell access to a web server. It can be set up on your own computer, but that is beyond the scope of this tutorial.

You will also need a WebSockets enabled browser in order to connect to the server. Currently (March 2012) that means using the latest release versions of Chrome or Firefox, or development versions of IE and Safari.

This tutorial will assume that your server is Unix-like (including Linux). If you have success (or even failure) with deploying this in Windows, I'd love to hear of your experience.

The PHP files are intended to be ran through your server's shell, so they should be placed somewhere other than in your web server's document root. I'll assume that they're placed in a directory under your home folder, ~/websock/.

The file websockets.php is the file that contains the server class, and 'users.php' contains the basic user class. These two are the only required file out of the files here. testwebsock.php implements a very basic echo server, and client.html contains the echo client.

To start, we want to write our own class which extends the WebSocketServer class, and that defines process(), connected(), and closed(). Open a blank document which we'll save as chatserver.php, and paste the following in:

#! /usr/local/bin/php
<?php

require_once('./websockets.php');

class ChatServer extends WebSocketServer {

    protected function process($user, $message) {
    
    }

    protected function connected($user) {

    }

    protected function closed($user) {

    }
}

$server = new ChatServer("0.0.0.0","9000");
try {
    $server->run();
}
catch (Exception $e) {
    $server->stdout($e->getMessage());
}

At this point, the server will run. It will accept connections just fine, and will receive data from the client.

However, it doesn't do anything with that data yet. That's your job.

For our purposes, we're just developing a very simple chat client. It will take messages and broadcast them to all of the connected clients.

Change the process() method to the following:

protected function process ($user, $message) {
	foreach ($this->users as $u) {
		$message = htmlspecialchars($message);
		$this->send($u,$message);
	}
}

Now, we can test it.

Go ahead and upload client.html to somewhere in your web server's document root (be sure to change the host URI first), and upload websockets.php, users.php, and your new chatserver.php to its own directory outside of the web server's document root. I'm assuming that you're putting it in ~/websock/, so the example shell commands will reflect that.

We'll also set our script to be executable, which will make it (slightly) easier to start, rather than invoking it from PHP.

[user@myhost ~]$ cd websock
[user@myhost websock]$ chmod +x ./chatserver.php
[user@myhost websock]$ ./chatserver.php
Server started
Listening on: 0.0.0.0:9000
Master socket: Resource id #6

The server will then sit quietly, waiting for connections. (In the near future, the script will allow daemonization -- that is, it will disconnect from the terminal and run in the background.)

Next, point your web browser at the client.html page on your server. It will automatically connect, and you'll be able to send data and see it return. Opening the page in multiple tabs or on multiple computers will allow you to see the broadcast functionality.

Finally, this is an extremely simple broadcast chat server being accessed through a client that is designed simply for testing an echo server. There is no way to tell who is sending what message, nor is there any user management going on, such as limiting the users to only authorized users (or at least reserving usernames to registered users as the public IRC servers do), or allowing operator permissions such as muting, banning, etc. In order to do that, you would want to extend the WebSocketUser class to hold details such as username, permissions, whether they're authenticated, etc., and you would also want to reference details in a database or some other permanent storage.

However, since that's more than just getting started, and closer to getting going, I'll leave the tutorial here.

Clone this wiki locally