How can I send "push notifications" to a specific users webpage? - javascript

I'm not really sure where to begin.
I'd like to be able to update a users current page when another user either updates my database or sends a specific GET/POST request (I could write it either way).
I was thinking server sent events but I made a quick test using my php server and realized a couple things. First I would need an event loop based server because using php I'd have to create a loop to keep checking my database for a specific change. Second I realized this would be very server intensive so I should look into another method.
So to explicitly ask my questions...
Is it possible to update a users current page when someone else sends a GET request to a php file on my server? How?
If I need an event loop based server what is my best option? node.js?
Is this possible with sockets if not with SSE? SSE makes more sense because I'm not looking for any user feedback.
Thanks

Websockets and something like SignalR for ASP.Net could help you. Check out the options here

You can use Ajax too.
Like this : Here

Related

Solution to instantly inform the user about the changes occurred on server

I'm trying to make an web application that will dynamically inform the user about the dynamic changes occur in server (php/mysql/apache).
I tried to implement a solution through APE (push) server but documentation is poor and the examples are not usable.
Does anyone know a solution that could be implemented to resolve this?
I forgot to add that besause of too many users and they need to constantly check the current status on server AJAX is not best solution.
AJAX was the first choice but we have to replace it.
I suggest you to use AJAX concepts for this purpose.
jQuery AJAX are very easy to implement with the help of Javascript setInterval() function.
You can find jQuery AJAX examples here.
All you need to do is write some PHP code which will return response which contains the changes occurred in Server.
In the Javascript layer check the received response and look for the changes. If it has any changes just populate the changes to the user.

Push a notification to another user

This question isn't a code-level issue but merely a functionality question / brainstorm.
Within my PHP script I want to send a notification to another user in real time, there's 1 way I've thought of to do this, if you know any better ones be sure to leave them in the comments!
My idea for this functionality is to insert into a databse table with the user's id and the message, then on the user's end constantly loop a select request looking for notifications corresponding to their id within $_SESSION, if it's found a message then delete it from the table and display it to the user.
This seems like it could "strain" my database and I'm wondering if there's a cleaner way to do this, it would also be much appreciated if somebody could post a javascript loop with a 3 second delay and an ajax post to a php file within it,
Thanks all,
James
The cleaner way to do this would be with websockets. Polling, long polling, and streaming are going to have exactly the problem you thought you were going to have.
The message recipient needs to be listening via broadcast also through websockets. The server will notify all websockets listening for that particular event.
You don't want to block with database read and writes. Just take the action from one user and send it to all the other websockets listening for that event (the other user's client side instances)
For event history you would consider persisting to a database with a message queue.
With a properly indexed, well-structured table, it won't be a strain to the db at all. Of course, this assumes your interval is reasonable (3 seconds you mentioned is great). That's how all real-time session-checking websites work. Those that need more than that, such as chat systems, basically anything that passes data more frequently and/or in larger packets, they use websockets.
Use Websockets or Server-Sent-Events
just an idea:
User-1 send message to the Server {"message" : "hello", "target" : "User-2"}
Server checks the message and redirect it to the target User
User-2 listening for events from Websocket or Server-Sent-Events

Sending popup message in the middle of session

I wonder is there a way to send users an alert message in the middle of a session? for example when a transaction status changes from "process" to "succeed" I want the user to know it in the middle of their session.
I hope my question can be answered using jQuery AJAX. Many thanks.
You should look into using Comet for things like this. I assume you meant that you want to alert the user without them having performed some action? The atmosphere project has a javascript component that can be used client side to achieve this, but it will also require some support from your back-end.

How google doc sync two document at the same time?

I am making a cooperation application like google docs, but I found this is different to do on the web. The problem is, when the user typing, another user should see the update at the same time. But what actually behind the screen? Is that when the user have an action, it sent a http request, and write into database. At the same time, another user get the action from database, and rendering the result that the user just type. If use this way to implement, the database need to keep read and write.....apart from this solution, how can I sync two people work on the fly? Thank you.
Check out something like this http://pusherapp.com/
Or http://www.tornadoweb.org/
Both are good at real time pushes without constant AJAX requests that will put a lot of strain on your server

Notify user on database change? JavaScript/AJAX

Yo.
I'm really quite new to this whole JavaScript business, not to mention AJAX, so I was thinking if you guys could help me out with a conundrum.
Basically, what I want is for the user to be notified upon a change in a value of a MySQL table. How should I go about that? Should I use jQuery, or can I slap something together myself?
Thankful for any and all replies.
Depending on how quickly you need the users to be notified, you could use either polling (sending a request every X seconds to see if there's anything new) or use comet.
In any case, you'll need a server side programming language to be querying the database and serving the results, and you'll need some javascript on the client-side to be sending requests and displaying responses. I would highly recommend using the jQuery library, since it simplifies a lot of cross-browser incompatibility.
Well, this is a bit abstract, but there are two parts to this: a server-side script, and your AJAX code (which sends this request to the script). Your server-side script will actually be doing the query to see if the database has changed, so your JavaScript will have to have a periodic execution (say once every ten seconds or whatever interval is right for you) if you are not waiting for the user to refresh their page.
The chain of events will look something like this:
AJAX -> Script -> DB -> Script -> AJAX -> Update Web Page
You will definitely want to use jQuery, Prototype, or some similar framework if you are new to AJAX. It will save you tons of time.
You should use jQuery and jQuery ajax method http://api.jquery.com/jQuery.ajax/
In the HTML page, the JavaScript will use AJAX to keep requesting a backend page during a certain interval.
The backend page will check if a MySQL database was changed.

Categories

Resources