Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Supply these to the `init` call (2nd argument)
{
debug?: false, // enable debug mode to log all info and errors
persistUser?: true | false // default value depends on environment, see below under "persisting users"
host?: "https://tracking.bucket.co", // don't change this
host?: "https://tracking.bucket.co",
sseHost?: "https://livemessaging.bucket.co"
}
```

Expand Down
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const TRACKING_HOST = "https://tracking.bucket.co";
export const ABLY_REST_HOST = "https://livemessaging.bucket.co";
export const ABLY_REALTIME_HOST = ABLY_REST_HOST;
export const SSE_REALTIME_HOST = "https://livemessaging.bucket.co";
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isForNode } from "is-bundling-for-browser-or-node";
import { version } from "../package.json";

import type { FeedbackPosition, FeedbackTranslations } from "./feedback/types";
import { TRACKING_HOST } from "./config";
import { SSE_REALTIME_HOST, TRACKING_HOST } from "./config";
import { defaultFeedbackPromptHandler } from "./default-feedback-prompt-handler";
import * as feedbackLib from "./feedback";
import {
Expand Down Expand Up @@ -45,6 +45,7 @@ export default function main() {
let debug = false;
let trackingKey: string | undefined = undefined;
let trackingHost: string = TRACKING_HOST;
let sseHost: string = SSE_REALTIME_HOST;
let sessionUserId: string | undefined = undefined;
let persistUser: boolean = !isForNode;
let liveSatisfactionActive: boolean = false;
Expand Down Expand Up @@ -112,6 +113,7 @@ export default function main() {

if (options.debug) debug = options.debug;
if (options.host) trackingHost = options.host;
if (options.sseHost) sseHost = options.sseHost;

if (options.feedback?.ui?.position) {
feedbackPosition = options.feedback?.ui?.position;
Expand Down Expand Up @@ -295,7 +297,7 @@ export default function main() {
userId,
body.channel,
(message) => handleFeedbackPromptRequest(userId!, message),
{ debug },
{ debug, sseHost },
);

feedbackPromptingUserId = userId;
Expand Down
26 changes: 18 additions & 8 deletions src/sse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from "cross-fetch";

import { ABLY_REALTIME_HOST, ABLY_REST_HOST } from "./config";
import { SSE_REALTIME_HOST } from "./config";

interface AblyTokenDetails {
token: string;
Expand All @@ -23,6 +23,7 @@ export class AblySSEChannel {
private userId: string,
private channel: string,
private ablyAuthUrl: string,
private sseHost: string,
private messageHandler: (message: any) => void,
options?: {
debug?: boolean;
Expand Down Expand Up @@ -75,9 +76,8 @@ export class AblySSEChannel {

private async refreshToken() {
const tokenRequest = await this.refreshTokenRequest();

const res = await fetch(
`${ABLY_REST_HOST}/keys/${encodeURIComponent(
`${this.sseHost}/keys/${encodeURIComponent(
tokenRequest.keyName,
)}/requestToken`,
{
Expand Down Expand Up @@ -176,7 +176,7 @@ export class AblySSEChannel {
const token = await this.refreshToken();

this.eventSource = new EventSource(
`${ABLY_REALTIME_HOST}/sse?v=1.2&accessToken=${encodeURIComponent(
`${this.sseHost}/sse?v=1.2&accessToken=${encodeURIComponent(
token.token,
)}&channels=${encodeURIComponent(this.channel)}&rewind=1`,
);
Expand Down Expand Up @@ -267,11 +267,21 @@ export function openAblySSEChannel(
userId: string,
channel: string,
callback: (req: object) => void,
options?: { debug?: boolean; retryInterval?: number; retryCount?: number },
options?: {
debug?: boolean;
retryInterval?: number;
retryCount?: number;
sseHost?: string;
},
) {
const sse = new AblySSEChannel(userId, channel, ablyAuthUrl, callback, {
debug: options?.debug,
});
const sse = new AblySSEChannel(
userId,
channel,
ablyAuthUrl,
options?.sseHost || SSE_REALTIME_HOST,
callback,
{ debug: options?.debug },
);

sse.open();

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Key = string;
export type Options = {
persistUser?: boolean;
host?: string;
sseHost?: string;
debug?: boolean;
feedback?: {
/**
Expand Down
Loading