cancel
Showing results for 
Search instead for 
Did you mean: 

How to prevent UI5 to open multiple Websocket connections?

DennisFullbier
Discoverer
0 Kudos

Hi,

I have that requirement to open only a single Websocket connection per client per application.

I want to use the sap.ui.core.ws.SapPcpWebSocket  implementation in UI5. But I have the issue that everytime the application opens in a new tab/window a new Websocket connection is opened. I can see multiple connections inside T-Code SMICM.

What I've tried:

I found that SharedWorker - Web APIs | MDN (mozilla.org) could be a solution, but I can't manage to run the Websocket code inside the SharedWorker API.

Component.js

 

var resourceUrl = sap.ui.require.toUrl("worker.js");
const worker = new SharedWorker(resourceUrl);
//or this
//const worker = new SharedWorker("worker.js");

 

worker.js (file exists right next to Component.js):

 

const ws = new sap.ui.core.ws.SapPcpWebSocket(`ws${location.protocol === "https:" ? "s" : ""}://${location.host}/sap/bc/apc/sap/z_apc`);

ws.onmessage = ({ data }) => {
};

onconnect = e => {
};

 

Whenever I want to run the SharedWorker constructur, I am getting this error:

DennisFullbier_0-1713173192025.png

Is there maybe another approach to prevent UI5 to open multiple Websocket connections?

Any suggestions would be helpful.

 

Accepted Solutions (0)

Answers (1)

Answers (1)

Dinu
Contributor
0 Kudos

You have to provide the module path to toUrl method.

var resourceUrl = sap.ui.require.toUrl(module_path + "/worker.js");

 

DennisFullbier
Discoverer
0 Kudos
Ok, I don't get the error "Failed to fetch worker script" anymore. But nevertheless the SharedWorker doesn't work as intended. The worker script is still not executed.