Robot image saying I Am Online

Figuring out your online status in Real World

Second Life has numerous nifty features, and despite some bugs, the urge to create all kinds of stuff has made me eagerly test for each and every one of them.

I wanted to get an “online status” on my web page, and this means using XML-RPC, a standard, open-source interface to inter-application communication. Linden Lab has provided a way for the “outside world” to communicate with scripts in Second Life and even has provided a 2-page description on how this could be accomplished. First, credit where credit’s due. I wouldn’t manage to know how to call XML-RPC from PHP (LL uses a Perl example, but my website runs from inside a PHP content management system) if I didn’t read this thread from Eddy Stryker, which uses a library provided by Keith Devens. You just have to put it somewhere in your web server and call it from there. (Note: You need to make sure that your php.ini configuration file has allow_call_time_pass_reference = On; newer versions of PHP, namely PHP5, have it off by default) Let us set up the object in Second Life. Just use any object at all and put the following script into it:

key avatar;
string online_status; // preserve it

default
{
  state_entry()
  {
    // don't exaggerate, one check per minute
    // is more than enough!
    llSetTimerEvent(60.0);
    llSetTouchText("XML/RPC");
    avatar = llGetOwner();
  }

  timer()
  {
    // now request agent data for online status
    avatar = llGetOwner();
    llRequestAgentData(avatar, DATA_ONLINE);
  }

  dataserver(key queryid, string data)
  {
    // this is the reply you get inside SL
    if (data == "1")
      online_status = "online";
    else
      online_status = "offline";

    // hover the text over your object
    llSetText(llKey2Name(avatar) + " is " + online_status, ZERO_VECTOR, 1.0);
  }

  touch_start(integer total_number)
  {
    // reset XML-RPC communications. Only
    // the owner can do this, and will
    // get a new channel key each time

    if (llGetOwner() == llDetectedKey(0))
    {
      llWhisper(0, "Registering with RPC server...");
      llOpenRemoteDataChannel();
    }
    else llWhisper(0, "Sorry, only " + llKey2Name(avatar)+ " may set up XML/RPC handler on object");
  }

  remote_data(integer event_type, key channel, key message_id, string sender, integer idata, string sdata)
  {
    // this is called from the Outside World
    llWhisper(0, "Incoming information request...");
    if (event_type == REMOTE_DATA_CHANNEL)
    {
      // as channels change, it's good to
      // get it once in a while...
      llWhisper(0, "Channel id: "+(string)channel);

      // send it to you each time it changes
      llEmail("[email protected]", "New channel id for Online Status handler", "New channel id is "+(string)channel);
    }
    if (event_type == REMOTE_DATA_REQUEST)
    {
      llRemoteDataReply(channel, channel, online_status, idata);
    }
  }
}
Code language: LSL (Linden Scripting Language) (lsl)

Notice that the way things work in Second Life, you need to know the channel id to communicate to your object (and this id changes over time, as objects are given away, scripts are reset, etc.). So, the best thing to do is to send the channel id by email!

Now, on your web site, first download Keith Devens’ library, save it as xmlrpc.inc where you can include it, and then use the following code somewhere inside your web site (you have to manually change the channel id below, and don’t forget to place the PHP tags before and after the code, which I cannot display here inside WordPress):

<?php
require("xmlrpc.inc");
$channel = "your-channel-id";
$strValue = ""; $intValue = 0;
$array["Channel"] = $channel;
$array["IntValue"] = $intValue;
$array["StringValue"] = $strValue;

list($success, $response) = XMLRPC_request('xmlrpc.secondlife.com', '/cgi-bin/xmlrpc.cgi', 'llRemoteData', array(XMLRPC_prepare($array)));
print "I am ";
if ($success) {
  print $response["StringValue"];
}
else
{
  print "having problems contacting RPC server...";
}
Code language: PHP (php)

You could do much more with this! Each message can send an integer and a string, and this means you could set up handlers for several different things, like tracking your position, listing your inventory, see what you’ve got attached, see who is nearby, etc. Basically, all information that you can collect inline, you can provide off-world, too! It’s just a question of replying to requests properly. Residents seem to be using this to track the status of their vending machines. Hmm, what a nifty idea!


Edit (June 5, 2020): If you’re trying to follow this article in Second Life, 16+ years after I’ve written it, things probably will break.

Print Friendly, PDF & Email