Connecting to Traccar Web sockets from a server

benrogmans8 years ago

Hi guys, first post from me so hello and Traccar is awesome :)

I would like to connect to the Traccar Web socket api (mywebapp:8082/api/socket) from a MeteorJS server

import { Socket } from 'socket.io-client';

HTTP.post('http://localhost:8082/api/session', {
    
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    content: "email=SOME_EMAIL&password=SOME_PASSWORD"

}, function(response, some){
    
    console.log(response);
                    
    var socket = new Socket('ws://localhost:8082/api/socket');
    
    console.log(socket);
}

The HTTP post response is ok and I can retrieve the JSESSIONID but I can not attach a cookie to the new Socket() request.

Is there a way to add the JSESSIONID as a query param instead of as a cookie? Or would you know a way for me to add a cookie to the Socket() connection?

Anton Tananaev8 years ago

Browser includes the cookie automatically. I'm not sure how to do it in Node.js, but there must be a way to supply an additional header (cookie).

benrogmans8 years ago

Yeah I thought that as well, but it looks like it's not possible :s

So just to be sure, there is no other way than sending a cookie header along for authentication?

I'm about to poll the http api every second :(

benrogmans8 years ago

Using this Node.js websocket library and this issue I have it working :)

https://github.com/websockets/ws/issues/473

Thanks for your reply anyways Anton.

Anton Tananaev8 years ago

Thanks for sharing the link.