Connect to WebSocket Example

NanaYaw3 years ago

im tyring to connect to websocket using the postman websocket client , below is the url

wss://[IP]:8082/api/socket

I get an error connection refused .

  1. Do i need to pass any token
  2. Can anyone provide a curl on how to connect using websocket
Anton Tananaev3 years ago

Why are you using wss instead of ws?

NanaYaw3 years ago

i just tried using ws , and i got the error below
Error: Unexpected server response: 503

Anton Tananaev3 years ago

I suspect that you don't have a user session.

NanaYaw3 years ago

Honestly i quite dont understand what you mean by "Session cookie is the only authorization option for WebSocket connection."
Because i've been able to create a user session calling /api/session.
but when i try connecting to the websocket i get the errors below

Error1
Error2

is it an authorization parameter i have to set called Session Cookie ?

Anton Tananaev3 years ago

I don't know what it's called in postman, but yes, you have to set session cookie in your request.

NanaYaw3 years ago

Okay i couldn't figure it out on Postman , but while using iOS swift URLSession Websocket i was able to pass cookies and it works .

Now you wrote that "In addition to Traccar REST API, we provide access to a WebSocket endpoint for live location updates and events. URL for the connection"

Does it mean that anytime a location change or device change is triggered , i will get a notification/message from the socket endpoint ?
Because i've been listening to the socket endpoint for a while , while doing some changes on the web app , but i don't get any message from the socket endpoint.

Any advise please ?

Anton Tananaev3 years ago

Did you receive any messages from device? You won't get updates if you just change device yourself.

NanaYaw3 years ago

Yes when i first connected to the websocket server i got message from device, but when im manually making changes i didnot receive any messages .

Anton Tananaev3 years ago

That's expected, as I said.