Realtime

public class Realtime

Realtime Messaging client for PHP

Snippets

Code

error_reporting(E_ALL);
session_start();
require('./ortc.php');

/* -------------------- */
/* REPLACE THESE VALUES */
/* -------------------- */ 
$URL = 'http://ortc-developers.realtime.co/server/2.1';
$AK = 'YOUR_APPLICATION_KEY';// your realtime.co application key
$PK = 'YOUR_APPLICATION_PRIVATE_KEY';// your realtime.co private key
$TK = 'dummyToken';// token: could be randomly generated in the session
$CH = 'myChannel'; //channel
/* -------------------- */
/*        END           */
/* -------------------- */
	
// ORTC auth
// on a live usage we would already have the auth token authorized and stored in a php session
// Since a developer appkey does not require authentication the following code is optional

if( ! array_key_exists('ortc_token', $_SESSION) ){
	$_SESSION['ortc_token'] = $TK;
	$rt = new Realtime( $URL, $AK, $PK, $TK );
	$ttl = 180000; // just a very large time to live
	$result = $rt->auth(
		array(
			$CH => 'w'
		),
    0,
		$ttl
	);//post authentication permissions. w -> write; r -> read
	print '\n';
	
}

// sending a message usage	
$result = $rt->send($CH, "The message to send", $response);
print '\n';

// enable presence with metadata
$result = $rt->enable_presence($CH, 1);

// get presence data
$presence_data = '';
$result = $rt->get_presence($CH, $presence_data);
echo($presence_data);

// disable presence
$result = $rt->disable_presence($CH);

Constructors

realtime Realtime(string balancer, string app_key, string priv_key, string token)

Creates a new instance of the ORTC Realtime class.

Parameters

string balancer

The Realtime Url.

string app_key

The application key.

string priv_key

The application private key

string token

The authentication token

Functions

boolean auth(Array channels, boolean private, Numeric expire_time, Array response)

Performs the authentication of a token for a set of channels.

Parameters

Array channels

The channels permissions. w -> write; r -> read; p -> presence

boolean private

The token is private.

Numeric expire_time

The token expire time in seconds.

Array response

The server response for debug purposes.

boolean send(string channel, string msg, Array response)

Sends the supplied message to the supplied channel.

Parameters

string channel

The channel name.

string msg

The message to send.

Array response

The server response for debug purposes.

boolean enable_presence(string channel, Numeric metadata, Array response)

Enables presence for the supplied channel

Parameters

string channel

The channel name.

Numeric metadata

0: Don't include the subscribers connection metadata
1: Include the subscribers connection metadata

Array response

The server response for debug purposes.

boolean disable_presence(string channel, Array response)

Disables presence for the supplied channel

Parameters

string channel

The channel name.

Array response

The server response for debug purposes.

boolean get_presence(string channel, string presence_data)

Gets the presence data for the supplied channel

Parameters

string channel

The channel name.

string presence_data

The channel presence data (JSON string)