views:

432

answers:

2

I'm trying to get some better logic behind this before I start writing code.

I have a model that collects new messages via a background process. (So the database gets updated every 2-3 mins with any new messages).

When the user comes to their inbox, they're presented with all their current messages. This is easy for me:

@user = User.find(params[:id])
@user.messages

And I show the user's current messages on the page.

However, what if a new message comes in while the user is on that page (or any other page), how can I update the page w/ the new message without requiring a full page refresh. I know this can be done via ajax (periodically_call_remote?) . But i'd like to only highlight (fade/blink) only the NEW message that comes in.

Trying to wrap my ahead around this. My current solution is to use a partial for the inbox, which calls another partial of inbox messages. But how can I update the page with just "NEW" messages periodically? Say, to check every 2 mins or so.

Ideas?

+1  A: 

If your messages have a timestamp then you can use that as part of the ID for the new message (HTML IDs aren't supposed to start with a number). This will uniquely identify that message on the page and let you highlight it using JavaScript etc.

John Topley
This is interesting John, I purposely put a timestamp on the records so that I could find all "new messages". (Messages with a timestamp greater than the last "update" call). However, How would I go about adding uniquely identifying them if each ID would be different? (Since each "new" message could have a different timestamp). Not sure if that makes sense...
Nick L
and then there's refreshing the page... im thinking maybe periodically_call_remote would be sufficient for that. Just thinking out loud here...
Nick L
Ah ha! just re-read my comment, didn't follow you at first but I think I get the idea. I'll add the timestamp to all id's of *new* messages and then highlight them that way. We'll see if that works..
Nick L
A: 

Why don't you use a periodic observer? (One that polls your app every X minutes)