What's the best way for the client app to immediately react to an update in the database?

前端 未结 4 488
Happy的楠姐
Happy的楠姐 2021-02-05 23:24

What is the best way to program an immediate reaction to an update to data in a database?

The simplest method I could think of offhand is a thread that checks the databa

相关标签:
4条回答
  • 2021-02-06 00:04

    I figure there must be some way, after all, web application like gmail seem to update my inbox almost immediately after a new email was sent to me. Surely my client isn't continually checking for updates all the time. I think the way they do this is with AJAX, but how AJAX can behave like a remote function call I don't know. I'd be curious to know how gmail does this, but what I'd most like to know is how to do this in the general case with a database.

    Take a peek with wireshark sometime... there's some google traffic going on there quite regularly, it appears.

    Depending on your DB, triggers might help. An app I wrote relies on triggers but I use a polling mechanism to actually 'know' that something has changed. Unless you can communicate the change out of the DB, some polling mechanism is necessary, I would say.

    Just my two cents.

    0 讨论(0)
  • 2021-02-06 00:10

    Well, the best way is a database trigger. Depends on the ability of your DBMS, which you haven't specified, to support them.

    Re your edit: The way applications like Gmail do it is, in fact, with AJAX polling. Install the Tamper Data Firefox extension to see it in action. The trick there is to keep your polling query blindingly fast in the "no news" case.

    0 讨论(0)
  • 2021-02-06 00:13

    You basically have two issues here:

    1. You want a browser to be able to receive asynchronous events from the web application server without polling in a tight loop.

    2. You want the web application to be able to receive asynchronous events from the database without polling in a tight loop.

    For Problem #1

    See these wikipedia links for the type of techniques I think you are looking for:

    • Comet
    • Reverse AJAX
    • HTTP Server Push

    EDIT: 19 Mar 2009 - Just came across ReverseHTTP which might be of interest for Problem #1.

    For Problem #2

    The solution is going to be specific to which database you are using and probably the database driver your server uses too. For instance, with PostgreSQL you would use LISTEN and NOTIFY. (And at the risk of being down-voted, you'd probably use database triggers to call the NOTIFY command upon changes to the table's data.)

    Another possible way to do this is if the database has an interface to create stored procedures or triggers that link to a dynamic library (i.e., a DLL or .so file). Then you could write the server signalling code in C or whatever.

    On the same theme, some databases allow you to write stored procedures in languages such as Java, Ruby, Python and others. You might be able to use one of these (instead of something that compiles to a machine code DLL like C does) for the signalling mechanism.

    Hope that gives you enough ideas to get started.

    0 讨论(0)
  • 2021-02-06 00:21

    Unfortunately there's no way to push data to a web browser - you can only ever send data as a response to a request - that's just the way HTTP works.

    AJAX is what you want to use though: calling a web service once a second isn't excessive, provided you design the web service to ensure it receives a small amount of data, sends a small amount back, and can run very quickly to generate that response.

    0 讨论(0)
提交回复
热议问题