How to check the database update from a browser?

Asked

Viewed 3,111 times

4

I’m trying to make a page that updates the <div> (that has the content brought from the database) automatically once some value is entered (as well as on this site).

I used the setInterval to update (with Ajax) to <div> every 10 seconds, but I didn’t like the result, because I don’t think it’s necessary to make requests every ten seconds.

So I wanted to do a check before, and in case you have any updates in the database run my script and update the <div>

How can I check if any value has been entered in the database?

  • 3

    Honestly this is the most wrong way to make an asynchronous request. I recommend you study Websockets. http://www.html5rocks.com/pt/tutorials/websockets/basics/.

  • I know this is the wrong way, so I asked for help, thanks @Luizpicolo! , I will give a study yes :D

2 answers

4


The model adopted by web servers before HTML 5 are one-way requests, which in practice means that the server can never send a request to the client, only the other way around. The server can only send data to a client in response to a request.

Current strategy: Polling

The tactic you’re using is called Polling, which consists in asking the server from time to time if "there is something new". This tactic is the only one possible when one side cannot initiate a request.

Using HTML5: Websockets

HTML 5 has introduced websockets. This technology allows the client to open a bidirectional with the server, and therefore receive or send data at any time.

The latest standardization of websockets is the rfc6455 and is supported by the following browsers:

  • Internet Explorer 10+
  • Mozilla Firefox 4+
  • Safari 5+
  • Google Chrome 4+
  • Opera 11+

If it is possible for your application to limit itself to these browsers, you can use websockets.

Using JAVA

It is also possible to use a Java applet to make two-way communication. This page shows a small example, with source code. The working method is similar to websockets - A connection to the web request part is opened with the server.

Using flash

You might be able to use flash as well. I’m not sure.

  • Do you have any idea how I would consult the bank with Websocket’s? i read this article http://www.html5rocks.com/pt/tutorials/websockets/basics/ but I did not understand it very well.

  • @Odair Has to implement two parts: the server (in its preferred language) and the client (in javascript). Looking over, the example shows only the client part. [This other example(http://turim.wordpress.com/2011/07/27/html-5-comunicacao-bi-directional.

  • Thanks for the help, man, I’m gonna read it and study it better.

  • @Odair :-)

1

If the desired information depends on specific tables, a Trigger could be created in the respective table (I will call table A) which, when having a record inserted, updated or removed, saves the log in another table (Table B). Your routine would consult Table B to find out if there was a change in the data and, if so, update your DIV.

Another way would be to schedule a JOB (SQL Server) that would verify the change of the data in Table A and this would popular Table B signaling the occurrence of change in the database.

That’s what I can glimpse now, quickly, but there must be other ways too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.