I have a React application for browser where I currently request data updates via the changes feed by calling the GET /{db}/_changes endpoint every 30 seconds. I want to upgrade to an asynchronous WebSocket connection.
However, when I try to establish with vanilla JS a WebSocket connection to the sgw server, I receive an error “WebSocket connection to ‘ws://my-sgw-api.com/db/_changes?feed=websocket’ failed”. No logs are written for the failed connection.
JS code for opening the WebSocket connection:
const client = new WebSocket(
'ws://my-sgw-api.com/db/_changes?feed=websocket'
);
client.onopen = () => {
console.log('WebSocket Client Connected');
client.send(JSON.stringify({
"limit": 10,
"include_docs": true,
"active_only": true,
"since": 0,
"heartbeat": 60000,
}))
};
On the other side, establishing the WebSocket connection from my Python backend works. The only difference that I see is that I am setting a cookie for authorisation in the Python code. However, it isn’t possible in JS to set cookies or auth headers in vanilla JS WebSockets.
A similar problem has already been adressed here: Authentication issue with WebSocket using SyncGateway Public REST API 2.8
What is the problem?
Do I have to pass authorisation parameters?
Is the WebSocket changes feed even supported for JS clients?
Thanks a lot for any help!