Hi everyone Im working on a symfony2 project,and my next step is to create a simple notification system,
when someone add insert a row in an entity, the others must be notified in the moment,
I wonder if I create a bloc of jquery code that get the date of the last created element and then do the rest work (show a notification ) I dont think is a good idea, or if there is a nice and a simple method to follow to create a notification system,
plz any idea could be helpful.
Thanks in advance!!!!
You shoul implement something like this:
Client send an ajax request with timestamp of last modification (first time sends 0)
Server compares timestamp of client to timestamp, to retrieve all messages with bigger timestamp than the one sent by user
If there are newer messages, return them immediately to the client, with the timestamp of the latest one On other hand, if there are no new messages, enter into a 2 minutes busy-wait loop, checking every 1-3 seconds (randomly) whether there are new messages.
When client receive servers answer, browser updates view and immediately sends a new ajax request.
Related
I'll try to keep this as simple as possible. Bear with me as I'm still quite inexperienced with Android.
I've created a side panel drawerLayout that displays a Friend List for users of my application. This includes sent requests and received requests:
So far, every time the activity is created, a PHP request to the remote server is populating this list with friend of all status types, like so:
MyAdapter adapter = new MyAdapter(this, populateFriendList());
I'm going to have buttons beside each friend in the list that can accept requests, decline them, etc. As an example: when confirming a friend, I've been able to remove them from the 'Sent Requests' section of the list view, and even update the database to reflect this.
But how am I supposed to update the ListView to then see the most up-to-date friendList, with the newly confirmed friend as a friend?
Worth noting is that rotating the device causes the friend list to update as the onCreate method is called again, meaning populateFriendList() gets called once more. I appreciate that this way probably isn't wise, but I'm not sure what I'm supposed to do instead.
There are a couple ways to handle this. You could use GCM to send a push notification when there is a new friend request, or you can do long polling in a service pinging your server every interval to see if there is a new request.
your rotation is calling onCreate() every time because thats what's happening in the activity. You can override OnConfigurationChanged() and that should handle rotation changes.
You can call onNotifyDataSetChanged() to let the adapter know there there is new data to be viewed.
I want build a notification system to my website, similar to Facebook. The notifications don't need to be in real time.
This is what I have in mind:
User create an event (upload a new photo, add a new comment, like a photo, or even an administration alert to all users)
Run a cronjob every 5 minutes to add the notifications into the notifications table: id|id_content|type_of_content_enum|affected_user_id|date|seen_bool
The cronjob, will run several functions for each type of notification, for example:
add_photos_notification() // This function will check all photos added in the past 5 minutes, and insert a row in the notification table, for each user following this person. The function will group all photos added into the past 5 minutes, so the follower don't get many notifications for the same type of content. Resulting in a notification like: User X added Y photos in his profile.
add_admin_notification() // This function will check all news added by the administration of the site in the past 5 minutes, and insert a row in the notification table, for each user on the system ...
Is this a correct approach to build a notification system?
Is it possible to miss an event, running a cron every 5 minutes where functions retrieve the past 5 minutes events?
To be safe, do you think an alternative could be checking all events where a field 'notified' is not true? The same function that will grab the events where 'notified' = false, update it to true after adding that notification into the notifications table.
Thanks,
I went with the cronjob route and working good so far. Since our system got so many users to be notified, I found it the most appropriate way to do for two reasons.
I don't need to edit my current scripts code, inserting functions to add notifications for every event I want notify.
Since there gonna be some actions where many users are affected, adding notifications in real time could result in long script delays and time outs.
I built a class called notifications and inside this class, there are functions to add notification for every event I want notify, for example: user_added_a_new_photo(); user_commented_on_a_photo();
For every notification generate, Im adding 1 entry per user to be notified. This is how my notifications db looks like:
id
affected_user_id //user being notified
user_generating_the_notification_id
content_type // enum containing all type of notifications my system has (example: photo, video, comment) ...
content_json // a json containing the notification content. Based on the content type, on the display view file, I call helpers that will format the notification row using the json info.
date // the date the notification was added
seen_on // the date the user saw the notification
clicked_on // if user clicked on the notification, store the date he clicked on it
display // true or false
For this purpose, I added the display field cause for every new notification I create, I check the database if the same user, has another not seen notification, from the same generating user. If this condition is true, I set the old notification to display = false, and group the two new notifications resulting in something like: User X added X new photos in his gallery.
The clicked_on field, stores the date the item was clicked so I can generate reports based on this info if I need to. When displaying the content, if this item is not null, I highlight the notification to mark those not checked yet.
I created a text field to store the notification content in json, cause different notifications has different styles to present the users. For example, a new comment notification, has only texts, but a new photo notification, has a preview thumb.
So far, no issue running it and working for my needs.
The only downside, since the cronjobs can only be run only every 1 minute, the notifications may have 1 minute delay. But since I don't need it in real time, I set the cronjob to run every 5 minutes.
I've been looking into something like this as well and i just found this question on stackoverflow
Building a notification system
Have a look at the answer, the user does shed a fair bit of light on the theory of how to implement this sort of system.
As far as i can see you would need to create a record or object for each notification, if 100 people are subscribe to that event then 100 records will be generated. When a page is loaded your system will find all the notifications that correspond to the user logged in (Maybe by record ID) and then you would notify the user of how many notifications they have.
Now another way of doing this (Note i haven't implemented any of these, they are just ideas) would be to make a notification in a a table, then we'd have a second table that will hold a users ID and a notification ID, a user will be added to this table when they are subscribed to a notification, for example, confirmation of a friend request.
Please note again, these aren't proven methods, they're a result of some of my research of the matter, i'd read the post i gave you before doing anything.
For my next application i would like to implement something that has a feature like the facebook wall but let me explain a bit. For those of you who used facebook you know that when somebody posts a message on your wall, and you are logged in to your account, you will get a notification immediately somewhere in the lower left corner. Lately they even pushed this a little bit further and if somebody comments on it the comments are updated as you visualize the page, it's like an instant chat.
My application will be developed in PHP, I will use Zend Framework to do it. I'm interested in the basic principle that makes the facebook wall behave like that (updates in real time). I know there is ajax involved but I can't really tell how is the javascript triggered when the user is doing something. Or even more, how to push back to a user some info that was added after he viewed the page. For example, let's say that a somebody adds me as a friend. I would like to see a notification saying "X has added you as a friend" if i am logged in. I hope you understand what I'm trying to do.
If you can tell me some basic ideas, maybe provide some links that have this information I would be very grateful.
Thank you for your time in reading this.
you need to look at comet , reverse ajax , ajax polling
If some event is triggered, then store the event on database (with ajax or without ajax).
You will be needing a script in server to check if some event has been triggered or not. This script should be able to check events that are stored in database.
You need to execute script in step 2 periodically. This can be acheived with with ajax (javascript or jquery) and a function settimeout (on javascript) to send ajax request to server periodically.
Changes are sent from server. So parse the response and update in page using javascipt and jquery.
So, it can be summarized as
Register an event (for one user)
Check the event (for other user)
Parse the response and update the page
There are several elegant ways to do this as answered by others.
The best would be the start the project and ask for help where ever stuck.
It is only partially possible to keep an HTTP connection open, so the best option is probably to poll for changes. You can send a request each second to see if anything is changed since time 'x'. On each response you send along the server time. With the new request you send the time of the old request and the server can return any events that happened inbetween.
Also you can read something about AMQP. You can send a message to recepients inboxes (after some actions in your system) and then read inboxes after start or with some time interval.
In SO, when your question got answer. or you got new badge, event is triggered. Or when you got new PM in forum, it also lets you know by alerting message.
You see message that something happened with your account when you enter site for first time after this event.
How is this implemented? How do scripts know, that they have something new to show you?
The programming technique you are looking for is called Comet. The link to wikipedia describes some implementations of that, but the easiest way is to make an XMLHttpRequest with a long timeout and only return data on change.
As an answer to your last comment
There are several tecniques to mark events as a 'new'. It can be another field in the database table, of boolean type: telling if event was shown to user or not. Or - easiest one - just a time of last user's visit being recorded in the session, and then al upcoming event's time being compared with it.
The event happens when another person does something like vote on your answer or question. This is recorded on the server side.
When you log on to the site the fact that some events occured while you were away can be determined from looking up the database for these records.
While you are on the site it is possible for the page to periodically poll the server for changes. So its not really the server that the event is triggered on.
im using comet to push new data to the user.
1: but i wonder how i should check if new data (new messages, new replies etc) is available?
should i in the php in the background use a while loop and sleep it for 1 min so it can check every one min if new data has come in?
or should i have a trigger in the database for this?
what available options are there?
2: and how can i actually check eg. if user got a new message with SQL? how should i set up "the system"?
1) I would use AJAX to ask for new messages from an opened user page. This way you do not need a script running on the background and only active users/pages need the results, so let them ask for it themselves.
2) If you send the date of the last message along with the AJAX request. It is fairly easy to check if the database contains newer messages with something like SELECT * FROM messages WHERE date > $last_message_date