Notify user on database change? JavaScript/AJAX - javascript

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.

Related

How to track progress of request submitted to the server via jQuery (ajax)?

I have some PHP app. At the moment I am developing part where an user submits a request/form to the server via ajax (jQuery). Server will insert thousands of records in MySQL database that will probably take couple minutes (due to various calculations etc).
Question: Is there a way to track progress of updates on server side? I understand I can write some output once everything is finished. A workaround would be something like:
- store somewhere session ID plus progress details (example: processed 3k inserts out of 5k - maybe in some progress table in database
- have front end jQuery script requesting that information from the server every 10 seconds or so...
Any ideas? Please feel free to ask any question you may have. Thank you.
While I don't know the specifics of what you're doing server-side, a script that takes a "couple minutes" to execute likely involves several distinct steps. It might make sense to have the script execute one step at a time, return its result, and then get called again to execute the next step.
By using a process like this, you'll be able to track progress client-side. The caveat is that if the client closes the browser window or leaves the page with your ajax code, the remaining steps won't be executed.

Proper way to pass server variables to client in ASP.NET WebForms

In the past, I usually have just embedded the values I need passed into html data elements and then read them from there via JavaScript. However, I know there's a more proper way to do so. In particular, I'd like to learn how implement the first method mentioned in this blog post. However, I'd also like to know what the best way to pass data from server-side to client side is when you're not using ajax.
EDIT: Since I'm still pretty unsure, let me explain what I'm trying do to. I'm creating a page with a lot of dynamic content on it that is dependent on reading from a database. I want to be able to load the page's static content first. Then, using jQuery, I want to make an ayschronous HTTP get request to the server to read from the database and retrieve the information I need. Finally, if I need more information from the database, I want to be able to request the server for more information without having to reload the page.
"When you're not using ajax"!?!? - Start!
If you're dead set on not making a service request to the server, there are the following ways:
Hidden form fields - so <input type=hidden value=xyz >
PageMethods - ok this is ajax, but it's simple, see here http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2>http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2
Best way.... is to do it properly, implement webapi and send down JSON then on the client end interpret that with Javascript.
Edit: your edit paints a different picture. I thought you just wanted a single piece of data, but it sounds like you need a proper api, so go with webapi, and use jQuery to make the calls, then populate through code, or jQuery templates(the best option).

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

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

Get parameter from another page using javascript?

I have a wordpress site, and i'm not a php developer and not very eager to start either so I'm avoiding it like the plague, but I do have a requirement that requires a little bit of extra coding. I need to:
go to a different website,
download that page,
check for a certain phrase,
if phrase exists, extract a link from that page
and if link is returned I need to show that link on my wordpress site.
Currently, I have an asp.net page that does this and i'm hosting that page in an iframe on my wordpress site. but i'd like to do it without an iframe.
Question is, is there anyway for javascript to go to a different page (my asp.net page) and get a parameter (link) from it. If link is provided i will show certain content on wordpress site.
Or can javascript download a text file from the server? problem with that is i need a trigger to update the text file.
Any advice is appreciated.
Thanks.
What you should understand is that by "avoiding [PHP] like the plague" you're inadvertently avoiding the proper way of doing things. Javascript is a client-side language, and PHP is a server-side language. By asserting that you only want the load on the client's end (the kind of logic involved in what you want to do isn't exactly lightweight), you can potentially end up with a VERY slow webpage.
Not to mention, this type of situation is analogous to using a hammer to do a backhoe's job.
Either way, to answer your question, yes. You can use the jQuery Load method in tandem with Javascript's Match method.
What you should TRY to do, however, is make a CURL request using PHP, and then cache the page on your server. By doing this, you will limit the number of calls to a given page, and optimize load times.
Please consider the second option, even as an attempt in good practice. Good luck.
Use ajax and connect to a different page (on your server) which is written in server-sided language (like asp.net, as you said), that connects to the remote website.
More about Ajax

How does Yahoo's new automatic loading work?

In the new Yahoo mail inbox, when u click the message it is displayed in a tab automatically (I guess with out server interaction). Does that mean Yahoo loads all the data first and then use them with java script when requested or not...anyho i don't have any idea and I would like if some one explain to me how it works since am planning to do the same with my application. I am sure this will boost application performance and i am eager to know.
I guess Yahoo did something similar to what Hotmail describes here
Basically they decide depending on several aspect what+when to preload...
I have not seen it but what you're describing sounds like dynamic AJAX loading. Basically, only load information when it is requested by the user. This will reduce network load and initial loading times. Most JS libraries have some form of AJAX helper. You can read more on AJAX here and here.
I am pretty sure it does have some server interaction. It definitely is using some sort of AJAX to fetch data from the server and show it to you. There are tons of tutorials about using AJAX which you can refer. You can probably start with http://w3schools.com/ajax/default.asp

Categories

Resources