Facebook LiveMessage API Tutorial
While developing an application for Facebook I came across the LiveMessage feature which is currently still in beta. The only problem is that I couldn’t find much information on how people actually used the API version of it. LiveMessage seems like it could be a really useful function to leverage for real time applications, not just chatting.
I know javascript okay but working under the FBJS framework forces you to reference the Facebook Documentation Wiki more than I’d prefer. They also prevent you from employing any JS frameworks, like prototype.js, forcing you to use their proprietary functions.
In this chat example I used javascript, PHP, FB API and FBJS.
First, just like the example in the wiki we create a listener. Here I also use the add_message_line function used in the example.
<script type="text/javascript">
<!--
var livemessage = new LiveMessage('test_event', function(data) {
add_message_line(data.name +": " + data.msg);
}
);
function add_message_line(line_text) {
var new_line = document.createElement('div');
new_line.setTextValue(line_text);
document.getElementById('chat').appendChild(new_line);
}
//-->
</script>
Then instead of using the send_message function in the wiki example I created my own function that does a POST to my own php file called chatTestSend.php which uses the facebook.livemessage.send() provided by the API. Doing it this way gave me a better understanding of what’s going on. I called this JS function sender(). Notice I used the FBJS Ajax object, you can read more about it here.
<script type="text/javascript">
<!--
function sender(uid, message){
var ajax = new Ajax();
var queryParams = { "uid" : uid, "message" : message };
ajax.responseType = Ajax.RAW;
ajax.requireLogin = true;
ajax.post("http://YOURDOMAIN/chatTestSend.php", queryParams);
ajax.ondone = function(data) {
add_message_line("Me: " + message);
document.getElementById('message').setValue('');
}
ajax.onerror = function(data) { add_message_line("Error"); };
}
//-->
</script>
If you haven’t figured it out already this function is going to be called by an html form on the onSubmit event. The code below is similar to the example on the wiki. Combine the above javascripts with this file and call it something like chatTest.html.
<form action="#" method="post" onsubmit="sender(document.getElementById('chat_with').getValue(), document.getElementById('message').getValue()); return false;" >
<div id="chat"></div>
Message:
<input id="message" name="message" size="40" type="text" />
To (user id):
<input id="chat_with" name="chat_with" type="text" />
<input type="submit" />
</form>
And lastly, the php code of chatTestSend.php would do something like the following. Create the FB object and get the logged in user. Then, if the posts variables aren’t empty get the user’s name. Then call the method. Since there is no method in the php client lib use call_method. The only thing special about facebook.livemessage.send is that the message is sent using JSON. Luckily php 5 provides us with the json_encode() function. Pass an associative array into the json_encode function and you’re good to go.
$this->facebook = new Facebook($this->FBAPIKEY, $this->FBSECRET);
$this->user = $this->facebook->require_add();
if(!empty($_POST) && !empty($_POST["uid"]) && !empty($_POST["message"]) ) {
$data = $this->facebook->api_client->users_getInfo($this->user, array('first_name'));
$result = $this->facebook->api_client->call_method("facebook.livemessage.send",
array("recipient"=>$_POST["uid"], "event_name"=>"test_event", "message"=>json_encode(array("from"=>$this->user, "msg"=>$_POST["message"], "name"=>$data[0]['first_name']) ) ) );
}
So that’s about it. Open up chatTest.html and post to chatTestSend.php and send some messages to yourself. I concede that all of this probably won’t work right out of the box but with a little knowledge and tweaking you should be able to get it right. Drop me a comment if you need some help.
























Hi, I’ve tried example straight from Wiki, and now this one. But I just can’t get it work. Message comes to receiver as GET value (debugged with Firebug), but it doesn’t show up to browser. From sender it seems like everything goes out fine.
Have they changed something since this or am I just doing it wrong?
Thanks
Sounds like you have a problem with your JS listener. As far as I know Facebook hasn’t changed anything on their end and my test app is still working. Read over your code again carefully, it’s possible that I made a mistake when writing up this example. Let me know if you figure out the problem.
Ok as an update here to solve this out if someone happens to read this:
Your code now works and so does the original from wiki. BUT LiveMessages currently only works on old Facebook, not in new layout. Hope they get that fixed soon, and I can finally enable all new features.
Hello, do you know how can I do this using a desktop application ?
great tutorial by the way.
I haven’t played around with this for a while. I haven’t even checked if the new facebook layout supports it. Can someone let me know about that?
I haven’t tried building a facebook desktop application yet. Sorry.
I’m trying to use the livemessage feature in my fbml code, but don;t see anything on my canvas page. How can I test if the feature is working?
I was wondering if some other user/friend accesses the appplication at the same time for me to be able to view it?
I believe you can test by sending your self a message. I honestly haven’t looked at FB stuff for some time now. Thanks for reading