elephant.io Fork me on GitHub
  • Home
  • Usage
  • Methods
  • Future

What is elephant.io ?

Elephant.io is a light and easy to use library that aims to bring some real-time functionality to a PHP application through socket.io and websockets.
It provides a socket.io client fully writtent in PHP that should be usable everywhere in your project.

Requirements

For elephant.io magic to operate, the following dependencies must be fulfilled :

  • Socket.IO 0.8.0
  • PHP 5.3
  • NodeJS 0.6.5

Close connection when finished (recommended)

The classic connection. It will allow you to connect to your socket.io instance and then emits something to it. It will then disconnect at the end of the script. We recommend using this for the time being.

                        
require('ElephantIOClient.class.php');

$elephant = new ElephantIOClient('http://localhost:1337');

$elephant->init();
$elephant->send(ElephantIOClient::TYPE_MESSAGE, null, null, 'Hello World!');
                        
                    

Making a persistent connection (not recommended)

For a persistent connection, here is an example of how to initiate the connection and emit an Hello World message. Once the message is sent, the connection will be kept with the socket.io server. It is still not recommended to use this at the time, as this feature is being at an early stage of development.

                        
require('ElephantIOClient.class.php');

$elephant = new ElephantIOClient('http://localhost:1337');

$elephant->init();
$elephant->send(ElephantIOClient::TYPE_MESSAGE, null, null, 'Hello World!');
$elephant->keepAlive();
                        
                    

Available Methods

init($keepalive = false)
Init will initialize the connection to socket.io

Parameters:
$keepalive - boolean - If true, the init launch keepAlive() at the end of the method



keepAlive()
keepAlive make a persistent connection with socket.io by sending back heartbeats.



read()
Read retrieve the latest incoming data.



send($type, $id = null, $endpoint = null, $message = null)
Send allow you to send message to socket.io.

Parameters:
$type - integer - the message type, you can use constants for more ease. Available constants:

  • self::TYPE_DISCONNECT
  • self::TYPE_CONNECT
  • self::TYPE_HEARTBEAT
  • self::TYPE_MESSAGE
  • self::TYPE_JSON_MESSAGE
  • self::TYPE_EVENT
  • self::TYPE_ACK
  • self::TYPE_ERROR
  • self::TYPE_NOOP
$id - integer - optionnal id used for ack
$endpoint - string - optionnal socket.io endpoint
$message - string - optionnal message to transmit

Example:
                        
$client->init();
$client->send(Client::TYPE_MESSAGE, null, null, ‘Hello World!’);
                        
                    


stdout($type, $message)
Allow you to print messages to stdout when executing in CLI.

Parameters:
$type - string - the type of your message must be one of the following:
debug,
info,
error,
ok
$message - string - the message to print

Example:
                        
$client->stdout(‘debug’, ‘connected!’);
                        
                    

Future Features :

  • More user-friendly methods to communicate with socket.io
  • Better callback management

More to come... Feel free to submit your features requests on our github issues page !


Elephant.io by Barreca Ludovic at Balloon. Licensed under MIT License - Copyright Balloon, Barreca Ludovic 2012