Nick L
Nick L

Reputation: 404

Rails: How can I update a page with new messages? via Ajax? Jquery?

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?

Upvotes: 1

Views: 799

Answers (2)

user62605
user62605

Reputation: 185

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

Upvotes: 0

John Topley
John Topley

Reputation: 115382

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.

Upvotes: 1

Related Questions