I have an AngularJS app with multiple controllers. Each controller needs to connect to the server to receive data, and I am using a websocket connection for this purpose. Initially, I created a new websocket connection in every controller:
ws = new WebSocket(wsUrl);
I used the same `wsUrl` for all connections. However, I encountered an issue where opening a second connection would cause the first one to stop responding. After some research, I found that it is better to establish one connection and use it across different controllers.
My question is - how can I achieve this? How can I utilize `websocket.onmessage` from different controllers?
ws.onmessage = function (evt) {
var data = angular.fromJson(evt.data);
if (data.ok == false) {
console.log('Error: {ok:false}');
} else if (data.content.type != 4) {
switch (data.state) {
case 'get_work_iterations':
listWorkIterations(data); // should be triggered in controller_1
break;
case 'updateIterations':
updateIterations(data); // should be triggered in controller_2
}
}
};