Best Way to get real-time location via API

Rogers Dwiputra7 years ago

Hi, i'm currently buid front end interface from traccar API, to get latest location i use /locations API and make ajax call with javascript setTimeout.

I want to ask is there any best way other than making ajax call periodicly.

Thank You

Anton Tananaev7 years ago

Best way is to use WebSocket like the official web app.

Rogers Dwiputra7 years ago

Ok so use WebSocket like HTML5 WebSocket and pull data from /locations API ?

Anton Tananaev7 years ago

I would recommend to look at the web app source code. Check Root.js class.

Rogers Dwiputra7 years ago

Thank You Anton, I will take a look

Rogers Dwiputra7 years ago

Hi Anton, i have try WebSocket /api/socket but I have to logged in traccar app to get socket message, when i logout traccar app the WebSocket return error 500.

So I have to pass user auth too when I make WebSocket connection? how to do that

Anton Tananaev7 years ago

You have to create a session and login.

Rogers Dwiputra7 years ago

Hi Anton, is to create session i just have to fire this API ?

https://www.traccar.org/api-reference/#operation--session-post

Or i have to save some information like token in the cookie?

Thank You

Anton Tananaev7 years ago

API is enough, but of course you need to save cookie as well to keep session.

Rogers Dwiputra7 years ago

Hi Anton, problem still same :

Response from WebSocket is 500 (Internal Server Error), but when I login traccar via web application and then I request WebSocket again it works, this is the code,

first I call POST /api/session and succeed (got response user information from server), and then make WebSocket call but response 500 (succeed if I logged in to traccar Web App)

$.ajax({
            url: "https://server/api/session",
            dataType: "json",
            type: "POST",
            data: {
                email: "usertest@public.com",
                password: "123456"
            },
            success: function(sessionResponse){
                console.log(sessionResponse);
                openWebsocket();
            }
        });

        var openWebsocket = function(){
            var ws;
            ws = new WebSocket('wss://server/api/socket');

            ws.onmessage = function (evt) 
           { 
              var received_msg = evt.data;
              dataparsed = JSON.parse(received_msg);
              if (dataparsed.positions) {
                  console.log("POSITIONS");	
              }
              if (dataparsed.devices) {
                  console.log("DEVICES");	
              }
              console.log(dataparsed);
              // console.log("Message is received...");
           };
            
           ws.onclose = function()
           { 
              // websocket is closed.
              console.log("Connection is closed..."); 
           };
                
           window.onbeforeunload = function(event) {
              socket.close();
           };
        };
Anton Tananaev7 years ago
Vasilev5 years ago

Hello,
i was trying this example - app.js.
i use:

var url = 'https://website';
var token = '........';

i dont have any data :) should i specify something else.. for the tracking devices, also can i get by tracking device ID..
can this token be generated dynamically or automatically, not via the web app with clicking on the button.

thank you!

redofi 4 years ago

Hi Anton Tananaev i'm working on a custom front end version of traccar,

can you please tell us how to integrate the sample code you provided https://github.com/tananaev/traccar-web/blob/master/web/simple/app.js

because i'm not receiving anything and i have nothing displayed and i'm somehow confused because your code doesn't provide authentification code to traccar

Please help us by explaining more this code and how to integrate it in order to display markers on map !
Thank you !

Anton Tananaev4 years ago

You have to use token.

redofi 4 years ago

Thank you for your answer !
I used the code you provided by replacing the value of URL and token but with no result

  var url = 'myserver_url:8082';
  var token = 'SIIGrX6K1IRx236KnBRL7OXRy9JEnV08';