Web Socket Connection - Traccar

Test Crook6 months ago

Hi,

Situation :

I have created a new account on Traccar and linked my mobile device to https://demo2.traccar.org. I have already checked, using the position API, that my device is linked to that server. Now, I'm trying to implement webSockets for further usage, as I want live location updates.

Implementation :

For the WebSocket implementation, I obtained the J-session ID from "/api/session/"

  Future<void> establishHttpSession() async {
    try {
      final response = await http.Client().post(
        Uri.parse('https://demo2.traccar.org/api/session'),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: {
          'email': 'testcrook8@gmail.com',
          'password': 'mySecuredPassword',
        },
      );

      print("Response we get : ${response.body.toString()}");

      if (response.statusCode == 200) {
        String cookie = response.headers['set-cookie']!.split(';')[0];
        print(cookie);
        establishWebSocketConnection(cookie);
      } else {
        print('${response.statusCode} ${response.reasonPhrase}');
      }
    } catch (error) {
      print('Error during HTTP session: $error');
    }
  }

From the above POST request, I got the JSESSIONID=node01an5mtdhrfzkvbzk3kkzi645r1007.node0

Now :

I am trying to create a WebSocket handshake, so I pass the same cookie within the headers, but I'm unable to make a successful connection...


  void establishWebSocketConnection(String cookie) {
    final headers = {
      HttpHeaders.cookieHeader: cookie,
    };
    _channel = IOWebSocketChannel.connect("ws://demo2.traccar.org/api/socket", headers: headers);
  }

Error :

Connection to {MY_SOCKET_URL} was not upgraded to WebSocket

I don't know what i am missing?

Don't refer me to the documentation, as I have already gone through it. Please refer me to the steps or the things I am missing.

Anton Tananaev6 months ago

You should be connecting to wss instead of ws.

Test Crook6 months ago

Thanks buddy, Now its providing the data. By the way what is the interval time (frequency) of getting the data. (Means after how many seconds I get the new result)

And can we change it ?

Thanks For Quick Answer
  1. Now If anyone is getting problem with the webSockets then you can mail me.
Anton Tananaev6 months ago

We don't allow sharing personal contact details on the forum.

Test Crook6 months ago

Okay, Aur Kya chl rha hai aaj-kl Traccar me... Sb Bdhiya?

Ashok Chandra5 months ago

@Test Crook, this code is not working after properly, session is working but not receving cookies is header, have you any full code

Test Crook5 months ago

@Ashok Chandra, Here's the full code of getting the traccar web socket implemented successfully., Hope it helps you ...

https://github.com/Sanjeev-ApniBus/traccar_web_socket

Ashok Chandra5 months ago

Thanks for sharing I will check

Ashok Chandra5 months ago

@Test Crock, I am still facing issues with code login is working successfully, But getting null value here my response

Response: {"id":47526,"attributes":{},"name":"Naresh Kumar","login":null,"email":"xxxxxxxxxxxx88@gmail.com","phone": null,"readonly":false,"administrator":false,"map":null,"latitude" :0.0,"longitude":0.0,"zoom":0,"twelveHourFormat":false,"coordinat eFormat":null,"disabled":false,"expirationTime":null,"deviceLimit ":-1,"userLimit":0,"deviceReadonly":false,"limitCommands":false," disableReports":false,"fixedEmail":false,"poiLayer":null,"totpKey ":null,"password":null}

Error during HTTP session: Unexpected null value.

if i am putting cookies manualy so getting this error

Error during HTTP session: Unsupported operation:
Platform._version

Test Crook5 months ago

If you are doing this in flutter, then provide me your code, only then i can identify the issue