Skip to content

feat: move websocket client to sdk, always restart ws client#278

Merged
spaenleh merged 1 commit intomainfrom
websockets
Oct 2, 2023
Merged

feat: move websocket client to sdk, always restart ws client#278
spaenleh merged 1 commit intomainfrom
websockets

Conversation

@mochiruntime
Copy link
Copy Markdown
Contributor

@mochiruntime mochiruntime commented Sep 26, 2023

Closes #268

Note: the WS client was copy pasted from query-client, however the restart mechanism bit was added:

// native client WebSocket instance
let ws: WebSocket;
const assignWsClient = () => {
console.debug('restarting ws client');
ws = new WebSocket(wsHost);
let keepAlive: ReturnType<typeof setInterval>;
// on close, recreate connection
ws.addEventListener('close', () => {
clearInterval(keepAlive);
assignWsClient();
});
// keep alive on error, trigger close event
ws.addEventListener('error', (error) => {
console.error(error);
ws.close();
});
// ad-hoc ping mechanism
ws.addEventListener('open', () => {
let ttl = 2;
keepAlive = setInterval(() => {
if (ttl <= 0) {
ws.close();
return;
}
// note: we abuse that sending a "ping" string will result in a bad request response
ws.send('ping');
ttl = ttl - 1;
}, 30000);
ws.addEventListener('message', () => {
ttl = 2;
});
});
return ws;
};
ws = assignWsClient();

Copy link
Copy Markdown
Member

@spaenleh spaenleh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Looks good to me ! Thank you @dialexo 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request v1.8.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lift websocket client to SDK

3 participants