Question and Answers: Using Drupal and the Live Update module
Navigation : Back to Blog
Tags :
Oh hello there, I didn't see you come in. I want to talk to you about getting started with the Live Update API module. Live Update is a module to give Drupal the ability to update a page when new content is available without requiring a page refresh.
We have done some Fun & Good Work over at PBS and my favorite part was the Live Chat system. We've also had the opportunity to contribute the backing API to Drupal as a module. There's been a bit of clamor to know How We Did It and that's what I want to cover today.
Let's create a Live Question & Answer system in Drupal!
We'll begin a base stack of:
- Drupal 6.x
- CCK (with Text fields)
- Views
- Live Update (currently it is a dev release and the best bet is checking out from CVS)
Now let's get started.
Setting up our Cotent Type & View
First create a new content type called "Question". We will add the following fields:
- A text area called Question (field_question_question)
- A text area called Answer (field_question_answer)
Now, let's set up a view to hold our questions. I called it 'chat_view'.
- Filter by node type of 'Question'
- Filter by content: Question IS NOT NULL
- Add fields: Question and Answer
- Add Page view (path: /chat)
- Set Row style to Node View
The next step is to create a couple sample questions & visit your view. You should see a list of questions.
Time to Make the Donuts
We are using drupal, which means configuration has gotten us 80% of the way there and we have to do some real live development work to finish the race. Let's dig in.
First download and enable the Live Update API module if you haven't already. Create a custom module called 'live_update_chat' and enable that.
We have to tasks to complete to make this system function.
- Telling Drupal to watch for new content.
- Telling Drupal that new content is available.
Telling Drupal to watch for new content.
We want to tell Drupal that new content maybe available when we are at our chat view.
We will be doing this by using hook_init(). This isn't best practice as we will have to make a check every page load. A better way is to use the Drupal 6.x Preprocessing Awesomness and load live_update when the view is being loaded. However, since preprocessing views from modules is currently complicated we will be using hook_init() for this exercise.
To tell Drupal to watch for new content we use the function
<span style="color: #000000"><span style="color: #0000BB"><?php<br />live_update_intialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">$default_settings </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span>$element (string) is a unique name to review to this instance of live_update.
$default_settings (array) is an array of settings to use by default it can be set per live_update, so we can leave this blank for now.
Our hook_init implementation should look like this.
<span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/** <br /> * Implementation of hook_init()<br /> **/<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">live_update_chat_init</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">// Tell drupal that we should watch for new content<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">) == </span><span style="color: #DD0000">'chat' </span><span style="color: #007700">&& !</span><span style="color: #0000BB">arg</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">live_update_initialize</span><span style="color: #007700">(</span><span style="color: #DD0000">'live-update-chat'</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span>Now if you visit our view (/chat) and you have Firebug installed, you can see in Console that Live Update looking for new content.
Telling Drupal new content is available
The last piece is to tell Drupal that we have new content available. We can use hook_nodeapi() to tell Live Update new content is available when a Question node with an Answer is saved.
This is done via a function
<span style="color: #000000"><span style="color: #0000BB"><?php<br />live_update_update_content</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">$html</span><span style="color: #007700">, </span><span style="color: #0000BB">$settings </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">)<br /></span><span style="color: #0000BB">?></span></span>$element string should be same you initialized with live_update_initialize
$html string is the updated content, it needs to be fully rendered html. Since we set the view to use Node Views as the row, we can call node_view to generate the same html a page reload would for that row.
$settings array is an array of settings that tell live_update how to insert the content. If this is not specified per update_content
<span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Implementation of hook_nodeapi() <br /> **/<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">live_update_chat_nodeapi</span><span style="color: #007700">(&</span><span style="color: #0000BB">$node</span><span style="color: #007700">, </span><span style="color: #0000BB">$op</span><span style="color: #007700">, </span><span style="color: #0000BB">$a3 </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">$a4 </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">$op </span><span style="color: #007700">== </span><span style="color: #DD0000">'presave' </span><span style="color: #007700">&& </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">type </span><span style="color: #007700">== </span><span style="color: #DD0000">'question'</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">field_question_answer</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'value'</span><span style="color: #007700">]) {<br /> </span><span style="color: #0000BB">$html </span><span style="color: #007700">= </span><span style="color: #0000BB">node_view</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">, </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">$settings </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'target' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'#node-'</span><span style="color: #007700">. </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">nid</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'replaceWith'</span><span style="color: #007700">,<br /> );<br /><br /> </span><span style="color: #0000BB">live_update_update_content</span><span style="color: #007700">(</span><span style="color: #DD0000">'live-update-chat'</span><span style="color: #007700">, </span><span style="color: #0000BB">$html</span><span style="color: #007700">, </span><span style="color: #0000BB">$settings</span><span style="color: #007700">);<br /> }<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span>Time to drink the nectar of our practice.
In one window load our view (/chat) and in the other edit one of the questions displayed on the view, fill in an answer and Live Update will pick up the status change update the page.
You can extend this several ways:
- Include cck fields to keep track when a question is 'Asked' or 'Answered'
- Build an admin interface to control a chat
- Extend the Q&A module to more 'chatting functionality'
Alright! So that is just one way you can use the Live Update API to build some cool stuff, I hope this gets you started on the right track. The project page is located here (the API documentation is included along with some sample modules). See you in the issue tracker!
Comments
Post a comment
Presentations
What we've coded
Videos
Upcoming workshops
-
San Francisco, CAFebruary 28, 2012
-
Denver, COMarch 19, 2012
-
Denver, COMarch 19, 2012







This module works better now. :)
"$html string is the updated content, it needs to be fully rendered html. Since we set the view to use Node Views as the row, we can call node_view to generate the same html a page reload would for that row."
Could you give an example on how to use this module on a view that is not a Node view? I'm having trouble getting this to work...
Wow!... I Like this Module.. it's great!
Wonderful!.. :-)
I don't understand... but it's necessary create new module?
Hi, I need help..
I have a node (page or others), and I want that people can insert comment without refresh.. or that page is like a live blogging page.
How I can do them?
I have found that there is hard conflict for JSON request in Drupal with CiviCRM!
nice!
What a great module! But why can't get it to work with my nodes?
Foundmoncler
Foundmoncler
Great module, thanks. Works like a charm
I want to have o block which displays live scores from soccer games (without reloading the page, of course). Can I do this with Live Update ? If yes, could you be so kind to tell me how ? I saw on Drupal official site that there are many users who want that kind of module.
Thanks for the great API
How to implement this in for views with Row Style: Fields?
I've looked around for this answer and no one has it...if you find it let me know!