Skip to content

Commit

Permalink
Updated socketId method to return base64 encoded connectionKey and cl…
Browse files Browse the repository at this point in the history
…ientId
  • Loading branch information
sacOO7 committed Sep 20, 2024
1 parent c50eba1 commit fbb8b63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/channel/ably/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion src/connector/ably-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fbb8b63

Please sign in to comment.