0

I have a multi-tenant Laravel web application, using stancl/tenancy for multi-tenancy support in Laravel. I also created a separate traccar server. Each tenant has a different user in the traccar server they have their own subdomains and each tenant have separates sessions. I have a problem about session cookie authentication I use the api POST api/session to create a session of the user and connect to traccar websocket and establish connection but it cannot work with my multitenancy setup.

This is my code for websocket connection:

 var ajaxTraccar = function (method, url, callback) {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = true;
            xhr.open(method, url, true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    callback(JSON.parse(xhr.responseText));
                }
            };
            if (method == 'POST') {
                xhr.setRequestHeader('Content-type', 'application/json');
            }
            xhr.send()
        };

    var openWebsocket = function(token){
        ajaxTraccar('GET', 'https://traccarwebsite.server/api/server', function(server) {
            ajaxTraccar('GET', 'https://traccarwebsite.server/api/session?token=' + token, function(user) {
                ajaxTraccar('GET', 'https://traccarwebsite.server/api/devices', function(devices) {

                    var socket = new WebSocket('wss://traccarwebsite.server/api/socket');

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

                    socket.onmessage = function (event) {
                        console.log("Socket Messaged);
                    };

                });
            });
        });
    };


    function initMap() {

        $.ajax({
            //url: "http://[IP]:8082/api/session",
            url: "https://traccarwebsite.server/api/session",
            dataType: "json",
            type: "post",
            async: false,
            data: {
                email: "{{ $email }}",
                password: "{{ $pass }}",
            },
            success: function(sessionResponse){
                openWebsocket(sessionResponse.token)
            }
            });
      }

Scenario: suppose I have two companies and open them at the same time. When I try to establish a WebSocket connection only one will connect and the other tenant can listen to the messages from the websocket.

How can I separate the WebSockets of tenants? Do I need to create a Proxy server?

Is this diagram viable?

Diagram

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
P7rck
  • 116
  • 1
  • 5
  • Where is the browser/user in this diagram? It does not seem to be on there, and thus I wonder why you are using WebSockets at all - for backend comms, could you not just use regular HTTP? – halfer Nov 23 '22 at 22:26

0 Answers0