Session cookie for WebSocket connection

Luciano Reis2 years ago

I'm hosting a custom web client consuming the REST API, the web client and the API endpoint are in different domains, how can I create sessions that allow me to connect to WebSocket?

I'm doing a POST to /session sending email and password and API access using basic authorization, it works great.
But how can I set these cookies so that I can connect to WebSocket and cross domain?

Anton Tananaev2 years ago

You probably have to configure CORS. Or just proxy API requests, so it's on the same domain.

By the way, why are you doing POST to /session if you use basic authorization after that?

Luciano Reis2 years ago

Where do I configure CORS in traccar to accept socket connection from another domain?

Anton Tananaev2 years ago

Configuration file. But I would recommend proxy option.

Luciano Reis2 years ago

Quando vocĂȘ fala em proxy, se refere ao pacote http-proxy-middleware?
Algo com isso:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = (app) => {
  app.use(createProxyMiddleware('/api/socket', { target: `ws://api.domain.com`, ws: true }));
  app.use(createProxyMiddleware('/api', { target: `http://api.domain.com}` }));
};
Gabriel Machadoa year ago

I have the same problem. Did you found a solution?