P7rck
P7rck

Reputation: 156

Traccar Websocket not connecting

I'm new to WebSockets and I want to have real-time data from traccar so I'm trying to use their WebSocket API but I couldn't connect to it. I followed their documentation https://www.traccar.org/traccar-api/.

And this is my code:

 $.ajax({
                //url: "http://[IP]:8082/api/session",
                url: "http://demo.traccar.org/api/session",
                dataType: "json",
                type: "POST",
                data: {
                    email: "[email protected]",
                    password: "myPassword"
                },
                success: function(sessionResponse){
                    console.log(sessionResponse);
                    openWebsocket();
                }
            });

            var openWebsocket = function(){
                var socket;
                socket = new WebSocket('ws://demo.traccar.org/api/socket');

                socket.onclose = function (event) {
                    console.log("WebSocket closed");
                };

                socket.onmessage = function (event) {
                    var i, j, store, data, array, entity, device, typeKey, alarmKey, text, geofence;
                    console.log(event.data);
                };
            };

and I'm encountering this error:

Error

Upvotes: 0

Views: 940

Answers (1)

Anton Tananaev
Anton Tananaev

Reputation: 2523

It looks like you're trying to send a request from a different host. It won't work because CORS is not enabled on the demo server.

The easiest workaround is set up a proxy, so both your JS app and the API are on the same host.

Upvotes: 0

Related Questions