phpsocket.io

PHP replacement for socket.io

What is it?

PHPSocket.IO is a library written in PHP/JavaScript that allows real-time communication between a client and server via web sockets.

In other words, it is very similar to NODE's socket.io. Easy to use, but powerful and customizable. An essential addition to your development toolbox.

Installation and config


Requirements

PHP 5.3+

Quick start

You need to start the server from the commandline and not from inside the browser.

$: php server/server.php 
From linux, you may want to use: nohup php server/server.php
The server is meant to be running continuously so that it waits for connection, if you close the server, then the functionality will stop.
After you have successfully started the server, you can run the client from the browser examples/basic/index.html or examples/advanced/index.html

Configuration

Before running the php server, you need to configure the port to run the socket installation on inside socketio.php

$server_port="2000";

Simple usage


1. The basic example server code (inside socketio.php)

$socket->on('chat message', function ($socket,$data,$sender) {
$socket->broadcast('chat message', $data,true);
})

The above code is a server event that occurs when a message is sent from the client. The message is broadcasted to everyone.

2. The basic example script

var socket=$.websocket('ws://162.144.68.201:2000');
	
$('form').submit(function() {
  socket.emit('chat message', $('#m').val());
  $('#m').val('');
  return false;
});
socket.on('chat message', function(msg){
    $('#messages').append($('<li>').text(msg));
});
socket.on('connect', function(user){
    $('#messages').append($('<li>').text("Welcome "+user));
});
	  
socket.listen();

When creating a connection to a server, you need to specify the server's ip address and the port number (inside the configuration of socketio.php explained earlier). If your server is example.com and you configure socketio.php to run on port 400 then you need to connect with $.websocket('ws://example.com:400'). However, you need to start your server before attempting connection with the client.
If you are running on localhost, then your server ip should be something like 127.0.0.1.

Online Examples

These examples utilize a multiple chatrooms where everyone can chat simultaenously. You can run any of these chats via different browser tabs, or different browsers or different devices.
No database storage is utilized in any forms in these examples. No session variables were used either.
  1. Click here to run the basic example.
  2. Click here to run the advanced example.
  3. Click here to run the chat im example.

Android Integration

For android users, please check out the links below:
  1. Android Chat Demo (apk download)
  2. Android Chat Sample (source code)
  3. Android Module (source Code)

Notes


phpsocket.io can re-use any existing MySQLi connection. To implement this library in developing your plugins, you need to look at the Advanced Start section and figure out how to get the MySQLi handle of your framework.

Issues and feature requests are very welcome.

Credits

Inspired by the general open-source community with special thanks to Github for making the life of programmers more fun.

Shoutouts

Check out an awesome developer portal created by yours truly.

Author

Anthony Ogundipe a.k.a. dhtml

Special thanks to Adewale Wilson for his contributions to this library.

License

phpsocket.io is licensed under the MIT License.