From fbb8b6343167250927a2f28d8f6d949a2603c26f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 20 Sep 2024 17:48:20 +0530 Subject: [PATCH] Updated socketId method to return base64 encoded connectionKey and clientId --- src/channel/ably/utils.ts | 9 ++++++++- src/connector/ably-connector.ts | 8 +++++++- src/echo.ts | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/channel/ably/utils.ts b/src/channel/ably/utils.ts index 04d9316c..4fa68b9e 100644 --- a/src/channel/ably/utils.ts +++ b/src/channel/ably/utils.ts @@ -33,13 +33,20 @@ export const toTokenDetails = (jwtToken: string): TokenDetails | any => { const isBrowser = typeof window === 'object'; -const toText = (base64: string) => { +export const toText = (base64: string) => { if (isBrowser) { return atob(base64); } return Buffer.from(base64, 'base64').toString('binary'); }; +export const toBase64 = (text: string) => { + if (isBrowser) { + return btoa(text); + } + return Buffer.from(text, 'binary').toString('base64'); +}; + const isAbsoluteUrl = (url: string) => (url && url.indexOf('http://') === 0) || url.indexOf('https://') === 0; export const fullUrl = (url: string) => { diff --git a/src/connector/ably-connector.ts b/src/connector/ably-connector.ts index f4a0a3c1..01b09911 100644 --- a/src/connector/ably-connector.ts +++ b/src/connector/ably-connector.ts @@ -2,6 +2,7 @@ import { Connector } from './connector'; import { AblyChannel, AblyPrivateChannel, AblyPresenceChannel, AblyAuth } from './../channel'; import { AblyRealtime, TokenDetails } from '../../typings/ably'; +import { toBase64 } from '../channel/ably/utils'; /** * This class creates a connector to Ably. @@ -118,9 +119,14 @@ export class AblyConnector extends Connector { /** * Get the socket ID for the connection. + * For ably, returns base64 encoded json with keys {connectionKey, clientId} */ socketId(): string { - return this.ably.connection.key; + let socketIdObject = { + connectioKey : this.ably.connection.key, + clientId : this.ably.auth.clientId, + } + return toBase64(JSON.stringify(socketIdObject)); } /** diff --git a/src/echo.ts b/src/echo.ts index 5e63590e..fd3f492f 100644 --- a/src/echo.ts +++ b/src/echo.ts @@ -117,6 +117,7 @@ export default class Echo { /** * Get the Socket ID for the connection. + * For ably, returns base64 encoded json with keys {connectionKey, clientId} */ socketId(): string { return this.connector.socketId();