Skip to content

Commit

Permalink
Add history sync URL to clients (browser + node) (#784)
Browse files Browse the repository at this point in the history
* Auto add history sync URL to client (browser)

* Auto add history sync URL to client (node)

* Add changeset
  • Loading branch information
rygine authored Jan 14, 2025
1 parent 84f045c commit cf6fbc0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/sharp-jars-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@xmtp/browser-sdk": patch
"@xmtp/node-sdk": patch
---

Add history sync URL to client
6 changes: 6 additions & 0 deletions sdks/browser-sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export const ApiUrls = {
dev: "https://dev.xmtp.network",
production: "https://production.xmtp.network",
} as const;

export const HistorySyncUrls = {
local: "http://localhost:5558",
dev: "https://message-history.dev.ephemera.network",
production: "https://message-history.production.ephemera.network",
} as const;
2 changes: 1 addition & 1 deletion sdks/browser-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { Conversation } from "./Conversation";
export type { MessageDeliveryStatus, MessageKind } from "./DecodedMessage";
export { DecodedMessage } from "./DecodedMessage";
export { Utils } from "./Utils";
export { ApiUrls } from "./constants";
export { ApiUrls, HistorySyncUrls } from "./constants";
export type * from "./types";
export * from "./utils/conversions";
export {
Expand Down
21 changes: 13 additions & 8 deletions sdks/browser-sdk/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export type NetworkOptions = {
* specific endpoint
*/
apiUrl?: string;
/**
* historySyncUrl can be used to override the `env` flag and connect to a
* specific endpoint for syncing history
*/
historySyncUrl?: string;
};

export type ContentOptions = {
/**
* Allow configuring codecs for additional content types
*/
codecs?: ContentCodec[];
};

/**
Expand All @@ -28,13 +40,6 @@ export type StorageOptions = {
dbPath?: string;
};

export type ContentOptions = {
/**
* Allow configuring codecs for additional content types
*/
codecs?: ContentCodec[];
};

export type OtherOptions = {
/**
* Enable structured JSON logging
Expand All @@ -55,6 +60,6 @@ export type OtherOptions = {
};

export type ClientOptions = NetworkOptions &
StorageOptions &
ContentOptions &
StorageOptions &
OtherOptions;
11 changes: 5 additions & 6 deletions sdks/browser-sdk/src/utils/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import init, {
getInboxIdForAddress,
LogOptions,
} from "@xmtp/wasm-bindings";
import { ApiUrls } from "@/constants";
import { ApiUrls, HistorySyncUrls } from "@/constants";
import type { ClientOptions } from "@/types";

export const createClient = async (
Expand All @@ -16,10 +16,6 @@ export const createClient = async (
await init();

const host = options?.apiUrl ?? ApiUrls[options?.env ?? "dev"];
// TODO: add db path validation
// - must end with .db3
// - must not contain invalid characters
// - must not start with a dot
const dbPath =
options?.dbPath ?? `xmtp-${options?.env ?? "dev"}-${accountAddress}.db3`;

Expand All @@ -33,13 +29,16 @@ export const createClient = async (
options.structuredLogging ||
options.performanceLogging);

const historySyncUrl =
options?.historySyncUrl ?? HistorySyncUrls[options?.env ?? "dev"];

return createWasmClient(
host,
inboxId,
accountAddress,
dbPath,
encryptionKey,
undefined,
historySyncUrl,
isLogging
? new LogOptions(
options.structuredLogging ?? false,
Expand Down
20 changes: 15 additions & 5 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export const ApiUrls = {
production: "https://grpc.production.xmtp.network:443",
} as const;

export const HistorySyncUrls = {
local: "http://localhost:5558",
dev: "https://message-history.dev.ephemera.network",
production: "https://message-history.production.ephemera.network",
} as const;

export type XmtpEnv = keyof typeof ApiUrls;

/**
Expand All @@ -50,6 +56,11 @@ export type NetworkOptions = {
* specific endpoint
*/
apiUrl?: string;
/**
* historySyncUrl can be used to override the `env` flag and connect to a
* specific endpoint for syncing history
*/
historySyncUrl?: string;
};

/**
Expand All @@ -70,10 +81,6 @@ export type ContentOptions = {
};

export type OtherOptions = {
/**
* Optionally set the request history sync URL
*/
requestHistorySync?: string;
/**
* Enable structured JSON logging
*/
Expand Down Expand Up @@ -132,6 +139,9 @@ export class Client {
level: options?.loggingLevel ?? LogLevel.off,
};

const historySyncUrl =
options?.historySyncUrl ?? HistorySyncUrls[options?.env ?? "dev"];

const client = new Client(
await createClient(
host,
Expand All @@ -140,7 +150,7 @@ export class Client {
inboxId,
accountAddress,
encryptionKey,
options?.requestHistorySync,
historySyncUrl,
logOptions,
),
signer,
Expand Down
2 changes: 1 addition & 1 deletion sdks/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type {
StorageOptions,
XmtpEnv,
} from "./Client";
export { Client, ApiUrls } from "./Client";
export { Client, ApiUrls, HistorySyncUrls } from "./Client";
export { Conversation } from "./Conversation";
export { Conversations } from "./Conversations";
export { DecodedMessage } from "./DecodedMessage";
Expand Down

0 comments on commit cf6fbc0

Please sign in to comment.