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
11 changes: 11 additions & 0 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ In the above example, the configuration would also fill in the following keys if
}
```

You may also pass a custom connector class instead of a built-in driver name. No defaults are merged in for custom connectors, so pass everything the connector needs:

```ts
import { configureEcho } from "@laravel/echo-react";
import { WaveConnector } from "laravel-wave";

configureEcho({
broadcaster: WaveConnector,
});
```

## Connection Status

You can get the current WebSocket connection status using the `useConnectionStatus` hook or the `echo().connectionStatus()` utility function.
Expand Down
34 changes: 25 additions & 9 deletions packages/react/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import Echo, { type BroadcastDriver, type EchoOptions } from "laravel-echo";
import Echo, {
type BroadcastDriver,
type Broadcaster,
type EchoOptions,
} from "laravel-echo";
import Pusher from "pusher-js";
import type { ConfigDefaults } from "../types";

let echoInstance: Echo<BroadcastDriver> | null = null;
let echoConfig: EchoOptions<BroadcastDriver> | null = null;
/**
* The Echo constructor argument, which allows a custom connector class as the broadcaster.
*/
type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0];

let echoInstance: Echo<keyof Broadcaster> | null = null;
let echoConfig: EchoConfig | null = null;

const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {
const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
if (echoInstance) {
return echoInstance as Echo<T>;
}
Expand All @@ -18,17 +27,19 @@ const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {

echoConfig.Pusher ??= Pusher;

echoInstance = new Echo(echoConfig);
echoInstance = new Echo<keyof Broadcaster>(echoConfig);

return echoInstance as Echo<T>;
};

/**
* Configure the Echo instance with sensible defaults.
*
* `broadcaster` accepts a built-in driver name or a custom connector class.
*
* @link https://laravel.com/docs/broadcasting#client-side-installation
*/
export const configureEcho = <T extends BroadcastDriver>(
export const configureEcho = <T extends keyof Broadcaster>(
config: EchoOptions<T>,
): void => {
const defaults: ConfigDefaults<BroadcastDriver> = {
Expand Down Expand Up @@ -73,18 +84,23 @@ export const configureEcho = <T extends BroadcastDriver>(
},
};

// Custom connector classes have no defaults to merge in...
const { broadcaster } = config;

echoConfig = {
...defaults[config.broadcaster],
...(typeof broadcaster === "string" ? defaults[broadcaster] : null),
...config,
} as EchoOptions<BroadcastDriver>;
} as EchoConfig;

// Reset the instance if it was already created
if (echoInstance) {
echoInstance = null;
}
};

export const echo = <T extends BroadcastDriver>(): Echo<T> =>
export const echo = <
T extends keyof Broadcaster = BroadcastDriver,
>(): Echo<T> =>
getEchoInstance<T>();

export const echoIsConfigured = () => echoConfig !== null;
22 changes: 22 additions & 0 deletions packages/react/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ describe("echo helper", () => {

expect(echoIsConfigured()).toBe(true);
});

it("creates Echo instance from a custom connector class", async () => {
const { configureEcho, echo } = await import("../src/config");

class CustomConnector {
options: unknown;

constructor(options: unknown) {
this.options = options;
}
}

configureEcho({
broadcaster: CustomConnector,
authEndpoint: "/custom/auth",
});

const instance = echo();

expect(instance.connector).toBeInstanceOf(CustomConnector);
expect(instance.options.authEndpoint).toBe("/custom/auth");
});
});
11 changes: 11 additions & 0 deletions packages/svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ In the above example, the configuration would also fill in the following keys if
}
```

You may also pass a custom connector class instead of a built-in driver name. No defaults are merged in for custom connectors, so pass everything the connector needs:

```ts
import { configureEcho } from "@laravel/echo-svelte";
import { WaveConnector } from "laravel-wave";

configureEcho({
broadcaster: WaveConnector,
});
```

## Connection Status

You can get the current WebSocket connection status using the `useConnectionStatus` function or the `echo().connectionStatus()` utility function.
Expand Down
34 changes: 25 additions & 9 deletions packages/svelte/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import Echo, { type BroadcastDriver, type EchoOptions } from "laravel-echo";
import Echo, {
type BroadcastDriver,
type Broadcaster,
type EchoOptions,
} from "laravel-echo";
import Pusher from "pusher-js";
import type { ConfigDefaults } from "../types";

let echoInstance: Echo<BroadcastDriver> | null = null;
let echoConfig: EchoOptions<BroadcastDriver> | null = null;
/**
* The Echo constructor argument, which allows a custom connector class as the broadcaster.
*/
type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0];

let echoInstance: Echo<keyof Broadcaster> | null = null;
let echoConfig: EchoConfig | null = null;

const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {
const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
if (echoInstance) {
return echoInstance as Echo<T>;
}
Expand All @@ -18,7 +27,7 @@ const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {

echoConfig.Pusher ??= Pusher;

echoInstance = new Echo(echoConfig);
echoInstance = new Echo<keyof Broadcaster>(echoConfig);

return echoInstance as Echo<T>;
};
Expand All @@ -28,9 +37,11 @@ export const echoIsConfigured = () => echoConfig !== null;
/**
* Configure the Echo instance with sensible defaults.
*
* `broadcaster` accepts a built-in driver name or a custom connector class.
*
* @link https://laravel.com/docs/broadcasting#client-side-installation
*/
export const configureEcho = <T extends BroadcastDriver>(
export const configureEcho = <T extends keyof Broadcaster>(
config: EchoOptions<T>,
): void => {
const defaults: ConfigDefaults<BroadcastDriver> = {
Expand Down Expand Up @@ -75,10 +86,13 @@ export const configureEcho = <T extends BroadcastDriver>(
},
};

// Custom connector classes have no defaults to merge in...
const { broadcaster } = config;

echoConfig = {
...defaults[config.broadcaster],
...(typeof broadcaster === "string" ? defaults[broadcaster] : null),
...config,
} as EchoOptions<BroadcastDriver>;
} as EchoConfig;

// Reset the instance if it was already created
if (echoInstance) {
Expand All @@ -87,5 +101,7 @@ export const configureEcho = <T extends BroadcastDriver>(
}
};

export const echo = <T extends BroadcastDriver>(): Echo<T> =>
export const echo = <
T extends keyof Broadcaster = BroadcastDriver,
>(): Echo<T> =>
getEchoInstance<T>();
22 changes: 22 additions & 0 deletions packages/svelte/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ describe("echo helper", () => {

expect(echoIsConfigured()).toBe(true);
});

it("creates Echo instance from a custom connector class", async () => {
const { configureEcho, echo } = await import("../src/config");

class CustomConnector {
options: unknown;

constructor(options: unknown) {
this.options = options;
}
}

configureEcho({
broadcaster: CustomConnector,
authEndpoint: "/custom/auth",
});

const instance = echo();

expect(instance.connector).toBeInstanceOf(CustomConnector);
expect(instance.options.authEndpoint).toBe("/custom/auth");
});
});
11 changes: 11 additions & 0 deletions packages/vue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ In the above example, the configuration would also fill in the following keys if
}
```

You may also pass a custom connector class instead of a built-in driver name. No defaults are merged in for custom connectors, so pass everything the connector needs:

```ts
import { configureEcho } from "@laravel/echo-vue";
import { WaveConnector } from "laravel-wave";

configureEcho({
broadcaster: WaveConnector,
});
```

## Connection Status

You can get the current WebSocket connection status using the `useConnectionStatus` composable or the `echo().connectionStatus()` utility function.
Expand Down
34 changes: 25 additions & 9 deletions packages/vue/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import Echo, { type BroadcastDriver, type EchoOptions } from "laravel-echo";
import Echo, {
type BroadcastDriver,
type Broadcaster,
type EchoOptions,
} from "laravel-echo";
import Pusher from "pusher-js";
import type { ConfigDefaults } from "../types";

let echoInstance: Echo<BroadcastDriver> | null = null;
let echoConfig: EchoOptions<BroadcastDriver> | null = null;
/**
* The Echo constructor argument, which allows a custom connector class as the broadcaster.
*/
type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0];

let echoInstance: Echo<keyof Broadcaster> | null = null;
let echoConfig: EchoConfig | null = null;

const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {
const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
if (echoInstance) {
return echoInstance as Echo<T>;
}
Expand All @@ -18,7 +27,7 @@ const getEchoInstance = <T extends BroadcastDriver>(): Echo<T> => {

echoConfig.Pusher ??= Pusher;

echoInstance = new Echo(echoConfig);
echoInstance = new Echo<keyof Broadcaster>(echoConfig);

return echoInstance as Echo<T>;
};
Expand All @@ -28,9 +37,11 @@ export const echoIsConfigured = () => echoConfig !== null;
/**
* Configure the Echo instance with sensible defaults.
*
* `broadcaster` accepts a built-in driver name or a custom connector class.
*
* @link https://laravel.com/docs/broadcasting#client-side-installation
*/
export const configureEcho = <T extends BroadcastDriver>(
export const configureEcho = <T extends keyof Broadcaster>(
config: EchoOptions<T>,
): void => {
const defaults: ConfigDefaults<BroadcastDriver> = {
Expand Down Expand Up @@ -75,16 +86,21 @@ export const configureEcho = <T extends BroadcastDriver>(
},
};

// Custom connector classes have no defaults to merge in...
const { broadcaster } = config;

echoConfig = {
...defaults[config.broadcaster],
...(typeof broadcaster === "string" ? defaults[broadcaster] : null),
...config,
} as EchoOptions<BroadcastDriver>;
} as EchoConfig;

// Reset the instance if it was already created
if (echoInstance) {
echoInstance = null;
}
};

export const echo = <T extends BroadcastDriver>(): Echo<T> =>
export const echo = <
T extends keyof Broadcaster = BroadcastDriver,
>(): Echo<T> =>
getEchoInstance<T>();
22 changes: 22 additions & 0 deletions packages/vue/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ describe("echo helper", async () => {

expect(echoIsConfigured()).toBe(true);
});

it("creates Echo instance from a custom connector class", async () => {
const { configureEcho, echo } = await import("../src/config");

class CustomConnector {
options: unknown;

constructor(options: unknown) {
this.options = options;
}
}

configureEcho({
broadcaster: CustomConnector,
authEndpoint: "/custom/auth",
});

const instance = echo();

expect(instance.connector).toBeInstanceOf(CustomConnector);
expect(instance.options.authEndpoint).toBe("/custom/auth");
});
});