Answer updated based on comments
This is going to be a little more work than you probably wanted it to be. You will need a database with 2 tables. Table will will hold active sessions IDs and table 2 will hold messages:
tblSessions
ID Int
LastSeen DateTime
SessionID Varchar(255)
tblMessages
ID Int
Timestamp DateTime
SessionID Varchar(255)
Message Varchar(255)
When a visitor comes to your page, you need to check if the visitor has a session ID. If the visitor does have a session ID already, update the LastSeen
column in the tblSessions
table. If the visitor doesn't have a session ID, assign on and add it to the tblSessions
table. This code should be run on all your pages when they are loaded.
You will need to run a query on the database table tblSessions
to remove all entries that have a LastSeen
older than some X time. The value of X should be say 5 min. This query could be run at the top of each page load, or in a server backend process.
Now, anytime you want to flash everyone's screens, you add an entry in tblMessages
for each entry in tblSessions
. Set the Timestamp
to the time you send the message, and set Message to "flash".
On the browsers side in javascript, setup a polling function with setInterval
. In your polling functions call an ajax script to a server side page to return any messages. This server side script should query the tblMessages
for any entries for the current session ID and pass them back. It should also remove the entries from the table.
Back in your javascript polling function you can check for the "Flash" message and flash the screen. The more frequently the polling function is called the more realtime your visitor will be, but more of a load will be but on your server.
Just like with the tbleSessions
table, you will want to remove old entries from the tblMessages
table if they are over say X +1 min time or you will get old results in the table that can cause issues down the road.
So.. This will flash the screen for anyone visiting your page, at roughly the same time. I say roughly because there is no way to flash at exactly the same time with network lag and everyone polling at slightly different times.... Well no easy way at any rate.