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?
What can I try next?