A lot of JavaScript is involved in doing this.  I see three possible
approaches:

1. Use a timer in JavaScript to cause the page to reload itself
frequently.  This isn't pretty.  The page will glitch as it reloads.  You
will be reloading the whole page each time, which includes much that the
browser already has.  There is a trade off between update latency and
useless request rate.  If your GPS updates come in at a dependable rate you
can make a good guess for polling rate.

Don't do this.

The only advantage that it has is that it will work with a browser that
doesn't support AJAX (read very old browser).

The Django code is easy.  No signals are required.  ORM does a fetch for
current coordinates for each request.  If you have lots of users you will
will have lots of database traffic, as well as lots of web traffic.  You
can reduce the database overhead by caching the current set of coordinates
in RAM (and maybe not use the database at all, if you don't need to keep
historical records.  If  you do use RAM, beware of threading issues on this
shared data.

2.  Use a time in JavaScript to trigger frequent AJAX requests for data
which has changed since the last data sent.

An outline is to have the server include a timestamp when it does send
data.  The request includes the timestamp, and if the server has no new
information, it sends an empty response (should be as cheap as a 304
response, but changing the response code is easy, and is arguably more
correct, but may or may not be harder on the JavaScript side).

Should work on all modern browsers.

The Django code is still easy.  In order to use RAM cache and still just
send what has changed, you will need to annotate each unit's position with
a timestamp (and it still wants to be the server timestamp, not the GPS
time of the sample) so that the view code can search for those newer than
the request's indicated time.  Think even harder about threading issues,
though I'm still pretty sure that I could do this without locks.

3.  Use a PUSH technique (see below).  The browser maintains a long
duration connection to the server.  When new data comes in, the server
distributes it to all connected browsers.  The chunk gets sent over the
connection to each browser, but the connection remains open for the next
data.  No timer is needed in JavaScript unless you want to indicate that
you haven't gotten data for a given unit in too long a time.

The downside is that you must support many simultaneous connections.  Any
of the OS, the front end (if used, e.g. Apache, nginx), or Django/python
could be a limiting factor here.  (I'm pretty sure that mod_wsgi can't
support WebSockets at all, at least at the moment.)  Otherwise the burden
on the server is very low, and latency is minimal.

The best, in my opinion, PUSH technique is WebSockets.  Since it part of
HTML5, it is probably here to stay, and even the usual suspect software
vender may implement (at least a usable sub-set) in the standard fashion.
Recent Firefox, Chrome, and, I believe, IE, and probably some others,
already support it.  All browsers will likely support it soon.  The
original technique was called COMET, but had some cross browser issues.
Another technique is called long poll.  If you really need wide browser
support now, look at the sockjs scheme.  A JavaScript library uses
WebSockets if available, but transparently falls back to older techniques
in preference order, and can even fall back to periodic polling (like
option 2).

At last fall's DjangoconUS there was talk of push for Django, but I don't
know where it has gotten.  If Django isn't there yet, and especially if
you're willing to insist on WebSockets capable browsers, you might consider
tornado, which supports WebSockets out of the box, instead of Django.  I'm
using tornado and WebSockets in a rather similar application.  (I'd be
happy to hear of progress in Django on the PUSH front.)

Bill




On Thu, Aug 1, 2013 at 10:54 AM, Saif Jerbi <m.jerbi.s...@gmail.com> wrote:

> I'm building a GPS Tracking System using Django, i should have a grid on
> my template that display real time information of cars (position,vitesse,
> temperature...). GPS send data to Postgres DataBase via a module that parse
> Data and saved it in specific table. What i need is how to make my Web App
> display data in real time? (when a row is inserted in Db, how can i display
> it in grid)
>
> i tried this package https://pypi.python.org/pypi/django-db-signals/0.1.1,
> but my DataParser is external from my django web app, it work independently
> from other side
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to