实时活动流程

I'm maintaining and old PHP based CMS that contains forum software, an article publishing system, among other things - all custom. Now the owners want to implement a global, pseudo real-time (it'll probably just update once every ten seconds or something) activity flow, similar to the one on Facebook:

enter image description here

Right now I'm trying to think of the best and easiest way to implement this in such a way that it can be easily extended to include different kinds of activities (creating blog posts, commenting on photo albums/blog posts, creating or replying to forum threads, publishing articles, etc.)

The front-end bit shouldn't be too hard, what I'm not sure about is how I should set up the events table to accommodate for all the different kinds of events that will be included. All the events will include things like a user ID, a URL, a datetime and an "event type", but I would have to save very different data depending on whether a user created a photo album or if someone replied to a forum thread. How do I solve this?

There are a few approaches you could take with this depending on whether each users' view is unique or not - for example: can they flag certain things as not interesting to them, or do you want to hide items they've already read. (I'm assuming in this answer that all users see the same feed.)

I wouldn't use a separate events table to store "actions" for the following reasons:

  • the information is only valid for a brief period of time (as long as it takes for an item to disappear off the top of the list) so you'd want to purge old values essentially creating a heavyweight FIFO buffer
  • you'd be storing a duplicate of existing data in your system (i.e. a record that says news story X was created at Y time when the record for the news story already has a creation time on it)
  • if an action is undone (or an item deleted) you would need to also delete the related entry in the events table
  • different types of events will have different information and metadata making it hard to normalise them all into a single table schema

You could:

  • run a scheduled script every 5 minutes or so that checks for any new activity (i.e. looks for new posts less than X minutes old) and writes a static HTML file with the activity feed contents that can be embedded in all pages where it should be visible
  • write your activities into an in-memory store (i.e. memcache) as and when they occur and flush entries older that your maximum action age at the same time
  • create a feed that fetches the latest action data on the fly but sits behind a proxy server that will cache it for a period of time... (this approach would also work well for per-user feeds)

UPDATE: Original poster has confirmed that all users see the same feed contents. Based on that I would recommend the simpest solution is schedule a script that generates a static HTML file of the feed that can be embedded straight into the page where needed (as it doesn't require installing and configuring an in-memory store or a proxy server).