Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

MM-32591: Reliable Websockets: Client side changes #7921

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions client/websocket_client.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {SocketEvents} from 'utils/constants';

const MAX_WEBSOCKET_FAILS = 7;
const MIN_WEBSOCKET_RETRY_TIME = 3000; // 3 sec
const MAX_WEBSOCKET_RETRY_TIME = 300000; // 5 mins

const reliableWebSockets = true;
Copy link
Member Author

Choose a reason for hiding this comment

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

This needs to be changed to actually take from the server config ServiceSettings.EnableReliableWebSockets


export default class WebSocketClient {
private conn: WebSocket | null;
private connectionUrl: string | null;
Expand All @@ -25,6 +29,7 @@ export default class WebSocketClient {
private missedEventCallback: (() => void) | null;
private errorCallback: ((event: Event) => void) | null;
private closeCallback: ((connectFailCount: number) => void) | null;
private connectionId: string | null;

constructor() {
this.conn = null;
Expand All @@ -39,8 +44,12 @@ export default class WebSocketClient {
this.missedEventCallback = null;
this.errorCallback = null;
this.closeCallback = null;
this.connectionId = '';
}

// on connect, only send auth cookie and blank state.
// on hello, get the connectionID and store it.
// on reconnect, send cookie, connectionID, sequence number.
initialize(connectionUrl = this.connectionUrl, token?: string) {
if (this.conn) {
return;
Expand All @@ -55,19 +64,25 @@ export default class WebSocketClient {
console.log('websocket connecting to ' + connectionUrl); //eslint-disable-line no-console
}

this.conn = new WebSocket(connectionUrl);
// Add connection id, and last_sequence_number to the query param.
// We cannot use a cookie because it will bleed across tabs.
// We cannot also send it as part of the auth_challenge, because the session cookie is already sent with the request.
this.conn = new WebSocket(connectionUrl + '?connection_id=' + this.connectionId + '&' + 'sequence_number=' + this.serverSequence);
this.connectionUrl = connectionUrl;

this.conn.onopen = () => {
this.serverSequence = 0;
// No need to reset sequence number here.
if (!reliableWebSockets) {
this.serverSequence = 0;
}

if (token) {
this.sendMessage('authentication_challenge', {token});
}

if (this.connectFailCount > 0) {
console.log('websocket re-established connection'); //eslint-disable-line no-console
if (this.reconnectCallback) {
if (!reliableWebSockets && this.reconnectCallback) {
agnivade marked this conversation as resolved.
Show resolved Hide resolved
this.reconnectCallback();
}
} else if (this.firstConnectCallback) {
Expand Down Expand Up @@ -123,6 +138,9 @@ export default class WebSocketClient {
this.conn.onmessage = (evt) => {
const msg = JSON.parse(evt.data);
if (msg.seq_reply) {
// This indicates a reply to a websocket request.
// We ignore sequence number validation of message responses
// and only focus on the purely server side event stream.
if (msg.error) {
console.log(msg); //eslint-disable-line no-console
}
Expand All @@ -132,9 +150,39 @@ export default class WebSocketClient {
Reflect.deleteProperty(this.responseCallbacks, msg.seq_reply);
}
} else if (this.eventCallback) {
if (msg.seq !== this.serverSequence && this.missedEventCallback) {
console.log('missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence); //eslint-disable-line no-console
this.missedEventCallback();
if (reliableWebSockets) {
// We check the hello packet, which is always the first packet in a stream.
if (msg.event === SocketEvents.HELLO && this.missedEventCallback) {
console.log('got connection id ', msg.data.connection_id); //eslint-disable-line no-console
agnivade marked this conversation as resolved.
Show resolved Hide resolved
// If we already have a connectionId present, and server sends a different one,
// that means it's either a long timeout, or server restart, or sequence number is not found.
// Then we do the sync calls, and reset sequence number to 0.
if (this.connectionId !== '' && this.connectionId !== msg.data.connection_id) {
console.log('long timeout, or server restart, or sequence number is not found.'); //eslint-disable-line no-console
this.missedEventCallback();
this.serverSequence = 0;
}

// If it's a fresh connection, we have to set the connectionId regardless.
// And if it's an existing connection, setting it again is harmless, and keeps the code simple.
this.connectionId = msg.data.connection_id;
}

// Now we check for sequence number, and if it does not match,
// we just disconnect and reconnect.
if (msg.seq !== this.serverSequence) {
console.log('missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence); //eslint-disable-line no-console
// We are not calling this.close() because we need to auto-restart.
this.connectFailCount = 0;
this.responseSequence = 1;
this.conn.close(); // Will auto-reconnect after MIN_WEBSOCKET_RETRY_TIME.
return;
}
} else {
if (msg.seq !== this.serverSequence && this.missedEventCallback) {
console.log('missed websocket event, act_seq=' + msg.seq + ' exp_seq=' + this.serverSequence); //eslint-disable-line no-console
this.missedEventCallback();
}
}
this.serverSequence = msg.seq + 1;
this.eventCallback(msg);
Expand Down