1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to implement notification system like on this forum

Discussion in 'PHP' started by ketting00, Aug 5, 2015.

  1. #1
    Hi guy,

    I want your advice on how to implement a notification system like on the right of this forum.

    The concept is you comment on something and leave the website.

    If other people make a comment on the same object behind you when you return to the website you get comment alert notification.

    I want it as simple as that.

    Some has suggested to put datetime stamp on every comment and and compare with latest time the user logio. I doubt if this would work if a computer remember a cookie.

    Any suggestion would be great.
    Thanks
     
    ketting00, Aug 5, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, what you can do is something like this:
    user logs in (user has to be logged in to make sure this works correctly)
    user comments on a post, or writes a post of his own - it gets stored in the database
    the current count of comments (either to the post in general, or just to the comment the user replied to, if that's the case) is stored in the database as well
    user logs out
    user logs back in, the backend checks for newer comments / counts the total of comments then checks for newer comments and display those
    this can of course also be done on a timer via ajax, so you can get new alerts while actually being on the site and logged in (or you could have a trigger in the database updating on certain events, like a new comment or post where the user has a flag set which then adds to a counter or similar with a link to the post/comment in question)

    It's not that hard to do, nor very complex, but it involves some db-work and the code for showing and retrieving the alerts.
     
    PoPSiCLe, Aug 5, 2015 IP
    ketting00 likes this.
  3. lasersgopew

    lasersgopew Member

    Messages:
    15
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    48
    #3
    The design of such a system would vary somewhat depending on the forum backend, so I'll just give a cursory blueprint.

    Server Side:
    Lets assume you have a normalized, relational database of threads and posts and whatnot; typical forum software.

    First you're gonna need a table for pending notifications and a table for subscriptions, think along the lines of [user_id, domain, value, hash], where domain is the type of event and value is the ID of the thread/page/video/etc; whatever you want to subscribe to.

    Once that's established, you'll need the related CRUD logic sorted out in your model layer, as well as a front controller and an API endpoint.

    From there you'd hook into the post logic and make it so that when a user posts in a thread:
    1. New alerts are generated for every user subscribed to that thread.
    2. The current user is subscribed to that thread.
    When a user views a thread they're subscribed to: any pending notifications are cleared.

    When a thread is deleted: subscriptions related to that thread are removed.

    When a notification is viewed: it is cleared (deleted or marked in some way).

    When more than n alerts exist for the same entity: they are collated in some way before sending them to the user.

    Client Side:
    Assuming you want active notifications that do not require reloading the page, there are a couple ways you can go about it. If you don't want active updates, just edit your view/template/whatever directly and ignore this section.

    Long-polling: Use AJAX to poll the notification endpoint periodically looking for new entries.
    WebSockets: Create a bi-directional connection to the user and push updates as they come.

    Or some combination thereof, each has its strengths and weaknesses.

    You'll also need the logic to format and display the alerts, as well as update the endpoint responsible for deleting them or marking them viewed.

    There are many other esoteric ways of doing this sort of thing, some better than others, but that's the gist of it.
     
    lasersgopew, Aug 5, 2015 IP
  4. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #4
    Thanks guys,

    What @PoPSiCLe suggests worked.

    I compare two dates. It can be done with MySQL query.

    Here's what I did.
    1. When user login, I retrieve his previous login date.
    2. Compare the date with newer comments. The result is comment count.
    3. When user click on the notification, I use ajax to update current date-time.

    It's working as expected.
     
    ketting00, Aug 5, 2015 IP
  5. lasersgopew

    lasersgopew Member

    Messages:
    15
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    48
    #5
    What happens when a user has posts in 500 different threads? Or if 500 users have posts in 500 threads and a number of them are viewing concurrently? What happens when a previously active user becomes inactive for a period of time, then returns to find his alerts don't load because the query timed out? Assuming you're using a relational database with table-level or row-level locking, what happens when the previously active user with the query that can never complete starts long-polling every n seconds and your database is locking thousands of rows, or even entire tables, while other users are trying to access those threads?

    It's also theoretically possible for an adversary to exploit such a system using two spam bots and posting in specific patterns, causing a denial of service. An attacker could also accumulate a decent number of alerts and (assuming you don't have a server-side request quota) flood the alerts endpoint, again causing a denial of service.
     
    lasersgopew, Aug 5, 2015 IP
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #6
    That's sound scary.

    Thanks you for bringing it up.

    I'll take that into consideration.
     
    ketting00, Aug 5, 2015 IP