Question and Answers: Using Drupal and the Live Update module

Profile picture

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:

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.

  1. Telling Drupal to watch for new content.
  2. 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

<?php
live_update_intialize
($element, $default_settings = NULL);
?>

$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.

<?php
/**
* Implementation of hook_init()
**/
function live_update_chat_init() {
 
// Tell drupal that we should watch for new content
 
if (arg(0) == 'chat' && !arg(1)) {
   
live_update_initialize('live-update-chat');
  }
}
?>

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

<?php
live_update_update_content
($element, $html, $settings = NULL)
?>

$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

<?php
/**
* Implementation of hook_nodeapi()
**/
function live_update_chat_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (
$op == 'presave' && $node->type == 'question') {
    if (
$node->field_question_answer[0]['value']) {
     
$html = node_view($node, TRUE, FALSE, TRUE);

     
$settings = array(
       
'target' => '#node-'. $node->nid,
       
'method' => 'replaceWith',
      );

     
live_update_update_content('live-update-chat', $html, $settings);
    }
  }
}
?>

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

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?

Add comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br/> <br /> <p> <img> <blockquote> <i> <b> <u>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Client Testimonial

Working with Chapter Three was great! They understood the development workflow perfectly and accommodated our needs to handle the theming and IA of the site. Add to this the short timeline and last minute changes they put up with. One area that Chapter Three really helped was setting up the development environment for us. This has proved invaluable to managing the site and pushing new features.

Michael Shaver, Web Administer, Intel Moblin Case Study

Drupalcon SF 2010