New message popup - php

I currently have a messaging system.
I am looking to make it so that when a user receieves a new message, they get an alert. A popup perhaps? Like a message box saying "you have a new message".
How could I achieve this?
Thanks

You can't really do this in php, as normal using of php implies no direct user interaction. What you need to do is to implement the relevant user interaction code in for example javascript, and do polling towards the server hosting the php (using AJAX or similar), to see if an popup should be shown or not.

This is not only a php problem. You need to make a database table like this:
messageID
contents
...
popuped
userID
Then make a page who gets the count of messages who are not popuped already:
SELECT COUNT(*) AS nr FROM messages WHERE userID = :userID AND popuped = 0
And after that set popuped to 1:
UPDATE messages SET popuped = 1 WHERE userID = :userID
And print the nr field.
Then for every page on your site you need to make a scheduled ajax request (for example every 20 seconds) who loadeds that page. If the result is higher as zero you need to show that the user has messages.
For the ajax request take a look for example at the mootools javascript framework (or jQuery, Prototype, etc.)

Are you looking for a 'live' message, as in an alert displays while the user is doing other things? Or are you looking for a 'static' message, as in an alert that displays when the user opens a page? The static method can be achieved with server-side php. The live method will need to employ client side techniques. I recommend using jQuery and jGrowl and something like jQuery Timers for the live methods. Try this SO question about javascript timers here

Related

pop up to another user of same application in php

I am using a php application. Here many users can login at same time like a web site.
Here a want when a user is saving form , a pop up should automatically appear on the browser of another user. id of 2nd user will be mentioned in form , when 1st user is savinf the form.
Please any body give me any idea.At least what type of technique i should use and what type of technology is required.
Thanks !
Rakesh
The technology you need is AJAX.
You should write a javascript function that calls periodically your server (through XMLHttpRequest) for any event you want to notify to your user,and when you have a relevant answer, that same function should pop up the warning.
You'll probably want to start using jQuery, a wonderful Javascript library (that includes Ajax calls) that will save you a lot of time.

How to refresh the Server or Database Table Values for every X Seconds

Ok i am tryin to make a Chatting application where users can chat with each other if theyr logged in to the website. I have set up everything except a little working which i am not able to write.
In my example : 2 users are chatting with each other : User A and User B
When User A submits a comment in the Chat Window , then the whole div containing the Comments should be refreshed not only at User A's side but also at User B's side so that when User A submits the comment, User B should be able to see it.
In short refreshing the div on every users page when a new record is added to the database table.
Im good in PHP, Jquery, AJAX.
Every x seconds use Ajax to check for new messages and if there is a new message, write it in the div.
There is no way for the server to tell the client a new message was posted. The client has to ask the server.
If you're comfortable restricting your users to new technologies, your best bet is to look into HTML5 websocket API. It's still a bit green around the gills, but it's certainly the way these things will be done in the near future. Kaazing's default example is a chat client, actually.
On the server side, there are implementations for websockets in JavaScript (Node.js), Java, and other technologies, but I'm not sure about PHP.
If you don't want to use websockets, research techniques that fall under the name "COMET" or search for "long polling". These will be far more responsive than a time interval.
If on the other hand you don't need it to be super-responsive, you could just use setInterval to have both clients poll the server for changes.
DC's answer is correct, if you are looking for an AJAX polling solution. This could be done with a simple JavaScript Interval.
However, polling is not light on resources as there are going to be a lot of duplicate content requests - and you would need to poll frequently to make the chat feel as though it is real-time.
If you would like to push data to the client from the server, I would look into writing this application using web sockets. NodeJS would be perfect for what you are trying to achieve, depending on what browsers you are wanting to support.
you need a timeout function like so:
function updateChatWindow(){
var xhr = $.ajax({
url : /* controller url */,
})
.done(function(response){
/** do something with the response the server gave you back **/
});
.fail(function(response){
/** handle a lost connection is some way, maybe show a message to the user, or retry request */
})
.then(function(){
periodicRefresh();
});
}
var timeoutFunction; //idealy, this will be inside a javascript object
//starts a refresh (in 500ms)
function periodicRefresh(){
timeoutFunction = setTimeout(updateChatWindow,500);
}
//stops the refresh from happening, see below why
function clearRefresh(){
clearTimeout(timeoutFunction);
}
var inputElement = $(/** the input area where the user types text **/);
inputElement.keyup(function(e){
if(e.keyCode==13){ // or whatever the e.???==13 was to check for enter
clearRefresh();
/** submit new chat line **/
updateChatWindow();
});
updateChatWindow();
Recap:
1) Have a ajax call that will update the window
2) Have a timer function that will store within it a timeout variable that will trigger a refresh of the content (the ajax call from 1)
3) Have the ability to cancel that request (because users type text, and after they hit enter, you should refresh the chat window no matter what)
4) Have the ability to stop the timeout function somehow to allow 3) to happen.

How to do notifications in PHP?

How can I show users that they have a new message or response awaiting in their inbox by showing them a number or text telling them to check it and then only remove the notification automatically if the user responds to it or clicks "done" (meaning no further response necessary).
I assume the messages will be stored in a DB. Just add a 'read' boolean column and then query the DB for unread messages to know whether there are new messages.
When the user replies you set the read column to 1.
This really needs javascript. Basically you have javascript make a request every x amount of seconds to a php page that checks for new notifications. Then the javascript alerts the user.
if you wan't something like on facebook, and other social network sites, U need to use javascript to do this. Php is serverside language, and that can;t do.
if you don't ask that, and u are only interested how to solve this with php and database.
Try to make new row. Called status, and update table set that coloumn is 1 if is read, 0 if isn't read...

How to update content based on user actions [like the facebook wall]

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.

How to make notification on message send like Facebook

How a notification can be shown after we are receiving a new message like FACEBOOK is displaying without refreshing a page. ( facebook is showing RED color notification after receiving a new message).
Thank You
There are two ways out there, you poll, or Comet.
Polling means you ask this question to your server periodically: "is there a new message?" with Ajax. If there is a new message, server returns it or the number of new messages.
Or you can implement some kind of Comet. Comet is a technology which contains open connections between client and server. Since there is an open connection, your server can send new information to clients.
After the number of new messages arrived to you, you can edit your DOM via javascript to show a red notification whereever you want.
You need to use setInterval to periodically check for any new updates and then, if they are found, use ajax to pull in the message.
Easiest way is to use jquery to update the content of a div at a sepecific interval...
#idofdivtoupdate
as it says.. the id of the div you want to auto update...
setinterval is in miliseconds.. (so 5000 checks every 5 seconds)
function updatecontent(){
$('#idofdivtoupdate').load('yourserverscript.php');
}
setInterval("updatecontent()", 5000 );
If ive understood you correctly you need to look up a technology called AJAX, its a combination of JavaScript and XML which allows the sending and receiving of data behind the scenes ie without needed to refresh the page. Theres plenty of tutorials/information out there about it and its not difficult to get the hang of, hope this helps.

Categories