I think I got your problem. When running internally it’s a http connection so you cannot replace https:// with wss://or do a strip.
You also have to handle http:// with ws://
Java example:
baseUrl = baseUrl.replace("http", "ws");
This would also explain why it is working locally but not deployed in Cumulocity.