Home VI. Server

VI. Server

Most of the time, you won’t touching the server unless you wanted to do some major customization. The server simply forward the message and control the heartbeat connection of each client.


a. Adding custom server functions

Open up the /Server/Function.php. The functions which declared as public are able to be called by the client. To define your own server function, just add it like this:

class Jsrs_Server_Function
   // Existing function here...
   // New function here...
   public function myFunction(Jsrs_Server_Message_Client $msg, Jsrs_Server_Connection $connection, $params = array())
       // Your function body here.
       // Send back your result through the $connection object.

The first parameter must be the client’s message object. Please refer to the API for details about what you can do with the Jsrs_Server_Message_Client object.

The second parameter will be the client’s connection object. This object hold the connection resources that allowed you to send message back to the client.

The last parameter is the function’s parameters passed by the client. It is optional. The parameter is an associative array. If the client did not specify the key of the value, the index of the array will be numeric in ascending order starting from 0. Eg: $params[0], $params[1], ....


Customizing server function required extensive knowledge on how JsRS works. It is better for you to familar with the whole framework before doing the customization.


b. Function APIs

Below is the list of functions available from the server. Please note that the parameters is the required parameter from the client.

Function’s name Parameter(s) Description Result
connect N/A Connect and register the client to the server. Event: onConnected(newClient, clientId)
disconnect N/A Disconnect the client and it’s connections. Event: onDisconnected()
getConnectedClients N/A Get a list of all connected client. Result:
“result” => array(
“id” => <id>,
“info” => array()
listen N/A Listening on the server. There will be no immediate result send back. It will hold the connection until there is a message for this client or expired.
replyCallback i.) message_id - The ID used to reference for the callback message.
ii.) result - The result value that going to send back.
To return the result back to the sender. Boolean. True upon successful.
plus Array of numbers. Eg: array(1, 2, 5, 23,...) This is a test function to do a summation of all the numbers provided in the database. Result:
“result” => <number>
Last modified on Monday, 26 July 2010 20:47