2020-08-11 16:24:59 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2019-12-04 11:45:49 +00:00
|
|
|
import WebSocketClient from '@gamestdio/websocket';
|
2017-02-03 23:34:31 +00:00
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
import { getAccessToken } from './initial_state';
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @type {WebSocketClient | undefined}
|
|
|
|
*/
|
|
|
|
let sharedConnection;
|
2018-05-18 00:32:35 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @typedef Subscription
|
|
|
|
* @property {string} channelName
|
|
|
|
* @property {Object.<string, string>} params
|
|
|
|
* @property {function(): void} onConnect
|
|
|
|
* @property {function(StreamEvent): void} onReceive
|
|
|
|
* @property {function(): void} onDisconnect
|
|
|
|
*/
|
2020-01-21 17:57:21 +00:00
|
|
|
|
2020-11-23 16:35:14 +00:00
|
|
|
/**
|
2023-04-30 00:29:54 +00:00
|
|
|
* @typedef StreamEvent
|
|
|
|
* @property {string} event
|
|
|
|
* @property {object} payload
|
|
|
|
*/
|
2018-05-18 00:32:35 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @type {Array.<Subscription>}
|
|
|
|
*/
|
|
|
|
const subscriptions = [];
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @type {Object.<string, number>}
|
|
|
|
*/
|
|
|
|
const subscriptionCounters = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Subscription} subscription
|
|
|
|
*/
|
|
|
|
const addSubscription = subscription => {
|
|
|
|
subscriptions.push(subscription);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Subscription} subscription
|
|
|
|
*/
|
|
|
|
const removeSubscription = subscription => {
|
|
|
|
const index = subscriptions.indexOf(subscription);
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
subscriptions.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Subscription} subscription
|
|
|
|
*/
|
|
|
|
const subscribe = ({ channelName, params, onConnect }) => {
|
|
|
|
const key = channelNameWithInlineParams(channelName, params);
|
|
|
|
|
|
|
|
subscriptionCounters[key] = subscriptionCounters[key] || 0;
|
|
|
|
|
|
|
|
if (subscriptionCounters[key] === 0) {
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-08-11 16:24:59 +00:00
|
|
|
sharedConnection.send(JSON.stringify({ type: 'subscribe', stream: channelName, ...params }));
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionCounters[key] += 1;
|
|
|
|
onConnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Subscription} subscription
|
|
|
|
*/
|
|
|
|
const unsubscribe = ({ channelName, params, onDisconnect }) => {
|
|
|
|
const key = channelNameWithInlineParams(channelName, params);
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
subscriptionCounters[key] = subscriptionCounters[key] || 1;
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-08-11 16:24:59 +00:00
|
|
|
if (subscriptionCounters[key] === 1 && sharedConnection.readyState === WebSocketClient.OPEN) {
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-08-11 16:24:59 +00:00
|
|
|
sharedConnection.send(JSON.stringify({ type: 'unsubscribe', stream: channelName, ...params }));
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionCounters[key] -= 1;
|
|
|
|
onDisconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
const sharedCallbacks = {
|
2023-04-03 01:31:39 +00:00
|
|
|
connected() {
|
2020-08-11 16:24:59 +00:00
|
|
|
subscriptions.forEach(subscription => subscribe(subscription));
|
|
|
|
},
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
|
|
|
received(data) {
|
2020-08-11 16:24:59 +00:00
|
|
|
const { stream } = data;
|
|
|
|
|
|
|
|
subscriptions.filter(({ channelName, params }) => {
|
|
|
|
const streamChannelName = stream[0];
|
|
|
|
|
|
|
|
if (stream.length === 1) {
|
|
|
|
return channelName === streamChannelName;
|
2017-11-15 15:04:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
const streamIdentifier = stream[1];
|
2019-03-07 21:17:52 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
if (['hashtag', 'hashtag:local'].includes(channelName)) {
|
|
|
|
return channelName === streamChannelName && params.tag === streamIdentifier;
|
|
|
|
} else if (channelName === 'list') {
|
|
|
|
return channelName === streamChannelName && params.list === streamIdentifier;
|
|
|
|
}
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
return false;
|
|
|
|
}).forEach(subscription => {
|
|
|
|
subscription.onReceive(data);
|
|
|
|
});
|
|
|
|
},
|
2018-05-18 00:32:35 +00:00
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
disconnected() {
|
2020-08-24 12:06:45 +00:00
|
|
|
subscriptions.forEach(subscription => unsubscribe(subscription));
|
2020-08-11 16:24:59 +00:00
|
|
|
},
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
reconnected() {
|
2020-08-11 16:24:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} channelName
|
|
|
|
* @param {Object.<string, string>} params
|
2023-04-30 00:29:54 +00:00
|
|
|
* @returns {string}
|
2020-08-11 16:24:59 +00:00
|
|
|
*/
|
|
|
|
const channelNameWithInlineParams = (channelName, params) => {
|
|
|
|
if (Object.keys(params).length === 0) {
|
|
|
|
return channelName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${channelName}&${Object.keys(params).map(key => `${key}=${params[key]}`).join('&')}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} channelName
|
|
|
|
* @param {Object.<string, string>} params
|
|
|
|
* @param {function(Function, Function): { onConnect: (function(): void), onReceive: (function(StreamEvent): void), onDisconnect: (function(): void) }} callbacks
|
2023-04-30 00:29:54 +00:00
|
|
|
* @returns {function(): void}
|
2020-08-11 16:24:59 +00:00
|
|
|
*/
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-08-11 16:24:59 +00:00
|
|
|
export const connectStream = (channelName, params, callbacks) => (dispatch, getState) => {
|
|
|
|
const streamingAPIBaseURL = getState().getIn(['meta', 'streaming_api_base_url']);
|
2024-05-22 14:45:18 +00:00
|
|
|
const accessToken = getAccessToken();
|
2020-08-11 16:24:59 +00:00
|
|
|
const { onConnect, onReceive, onDisconnect } = callbacks(dispatch, getState);
|
|
|
|
|
2024-05-22 14:45:18 +00:00
|
|
|
if(!accessToken) throw new Error("Trying to connect to the streaming server but no access token is available.");
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
// If we cannot use a websockets connection, we must fall back
|
|
|
|
// to using individual connections for each channel
|
|
|
|
if (!streamingAPIBaseURL.startsWith('ws')) {
|
|
|
|
const connection = createConnection(streamingAPIBaseURL, accessToken, channelNameWithInlineParams(channelName, params), {
|
2023-04-03 01:31:39 +00:00
|
|
|
connected() {
|
2020-08-11 16:24:59 +00:00
|
|
|
onConnect();
|
2017-11-15 15:04:15 +00:00
|
|
|
},
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
received(data) {
|
2017-11-15 15:04:15 +00:00
|
|
|
onReceive(data);
|
|
|
|
},
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
disconnected() {
|
2020-08-11 16:24:59 +00:00
|
|
|
onDisconnect();
|
|
|
|
},
|
2019-03-07 21:17:52 +00:00
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
reconnected() {
|
2019-03-07 21:17:52 +00:00
|
|
|
onConnect();
|
2017-11-15 15:04:15 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
return () => {
|
|
|
|
connection.close();
|
2017-11-15 15:04:15 +00:00
|
|
|
};
|
2020-08-11 16:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const subscription = {
|
|
|
|
channelName,
|
|
|
|
params,
|
|
|
|
onConnect,
|
|
|
|
onReceive,
|
|
|
|
onDisconnect,
|
|
|
|
};
|
|
|
|
|
|
|
|
addSubscription(subscription);
|
|
|
|
|
|
|
|
// If a connection is open, we can execute the subscription right now. Otherwise,
|
|
|
|
// because we have already registered it, it will be executed on connect
|
|
|
|
|
|
|
|
if (!sharedConnection) {
|
|
|
|
sharedConnection = /** @type {WebSocketClient} */ (createConnection(streamingAPIBaseURL, accessToken, '', sharedCallbacks));
|
|
|
|
} else if (sharedConnection.readyState === WebSocketClient.OPEN) {
|
|
|
|
subscribe(subscription);
|
|
|
|
}
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
return () => {
|
|
|
|
removeSubscription(subscription);
|
|
|
|
unsubscribe(subscription);
|
2017-11-15 15:04:15 +00:00
|
|
|
};
|
2020-08-11 16:24:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const KNOWN_EVENT_TYPES = [
|
|
|
|
'update',
|
|
|
|
'delete',
|
|
|
|
'notification',
|
|
|
|
'conversation',
|
|
|
|
'filters_changed',
|
|
|
|
'announcement',
|
|
|
|
'announcement.delete',
|
|
|
|
'announcement.reaction',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {MessageEvent} e
|
|
|
|
* @param {function(StreamEvent): void} received
|
|
|
|
*/
|
|
|
|
const handleEventSourceMessage = (e, received) => {
|
|
|
|
received({
|
|
|
|
event: e.type,
|
|
|
|
payload: e.data,
|
|
|
|
});
|
|
|
|
};
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @param {string} streamingAPIBaseURL
|
|
|
|
* @param {string} accessToken
|
|
|
|
* @param {string} channelName
|
|
|
|
* @param {{ connected: Function, received: function(StreamEvent): void, disconnected: Function, reconnected: Function }} callbacks
|
2023-04-30 00:29:54 +00:00
|
|
|
* @returns {WebSocketClient | EventSource}
|
2020-08-11 16:24:59 +00:00
|
|
|
*/
|
|
|
|
const createConnection = (streamingAPIBaseURL, accessToken, channelName, { connected, received, disconnected, reconnected }) => {
|
|
|
|
const params = channelName.split('&');
|
2017-11-15 15:04:15 +00:00
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-08-11 16:24:59 +00:00
|
|
|
channelName = params.shift();
|
2020-01-21 17:57:21 +00:00
|
|
|
|
|
|
|
if (streamingAPIBaseURL.startsWith('ws')) {
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
2020-01-21 17:57:21 +00:00
|
|
|
const ws = new WebSocketClient(`${streamingAPIBaseURL}/api/v1/streaming/?${params.join('&')}`, accessToken);
|
|
|
|
|
2023-04-03 01:31:39 +00:00
|
|
|
// @ts-expect-error
|
|
|
|
ws.onopen = connected;
|
|
|
|
ws.onmessage = e => received(JSON.parse(e.data));
|
|
|
|
// @ts-expect-error
|
|
|
|
ws.onclose = disconnected;
|
|
|
|
// @ts-expect-error
|
2020-01-21 17:57:21 +00:00
|
|
|
ws.onreconnect = reconnected;
|
|
|
|
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
channelName = channelName.replace(/:/g, '/');
|
|
|
|
|
|
|
|
if (channelName.endsWith(':media')) {
|
|
|
|
channelName = channelName.replace('/media', '');
|
|
|
|
params.push('only_media=true');
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:57:21 +00:00
|
|
|
params.push(`access_token=${accessToken}`);
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
const es = new EventSource(`${streamingAPIBaseURL}/api/v1/streaming/${channelName}?${params.join('&')}`);
|
2020-01-21 17:57:21 +00:00
|
|
|
|
|
|
|
es.onopen = () => {
|
2020-08-24 12:06:45 +00:00
|
|
|
connected();
|
2020-01-21 17:57:21 +00:00
|
|
|
};
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
KNOWN_EVENT_TYPES.forEach(type => {
|
2023-04-03 01:31:39 +00:00
|
|
|
es.addEventListener(type, e => handleEventSourceMessage(/** @type {MessageEvent} */(e), received));
|
2020-08-11 16:24:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
es.onerror = /** @type {function(): void} */ (disconnected);
|
2017-02-03 23:34:31 +00:00
|
|
|
|
2020-01-21 17:57:21 +00:00
|
|
|
return es;
|
2017-02-03 23:34:31 +00:00
|
|
|
};
|