mirror of
https://github.com/nvms/prsm.git
synced 2025-12-15 15:50:53 +00:00
subscribe -> subscribeChannel
This commit is contained in:
parent
c492ee8a05
commit
66803c1177
@ -202,7 +202,7 @@ The `history` parameter tells Mesh to store the message in a Redis list (`histor
|
||||
### Client usage
|
||||
|
||||
```ts
|
||||
const { success, history } = await client.subscribe(
|
||||
const { success, history } = await client.subscribeChannel(
|
||||
"chat:room1",
|
||||
(message) => {
|
||||
console.log("Live message:", message);
|
||||
@ -218,7 +218,7 @@ if (success) {
|
||||
Unsubscribe when no longer needed:
|
||||
|
||||
```ts
|
||||
await client.unsubscribe("chat:room1");
|
||||
await client.unsubscribeChannel("chat:room1");
|
||||
```
|
||||
|
||||
This feature is great for:
|
||||
@ -275,7 +275,7 @@ When presence is enabled for a room, Mesh automatically:
|
||||
### Getting presence information (server-side)
|
||||
|
||||
```ts
|
||||
// Get all connections currently present in a room
|
||||
// get all connections currently present in a room
|
||||
const connectionIds = await server.presenceManager.getPresentConnections("lobby");
|
||||
```
|
||||
|
||||
@ -454,14 +454,14 @@ server.exposeRecord(/^private:.+$/, async (conn, recordId) => {
|
||||
To allow clients to _subscribe_ and also _modify_ records, use `exposeWritableRecord`. This also accepts optional guard functions to control _write_ access:
|
||||
|
||||
```ts
|
||||
// Allow any connected client to write to cursor records
|
||||
// allow any connected client to write to cursor records
|
||||
server.exposeWritableRecord(/^cursor:user:\d+$/);
|
||||
|
||||
// Allow only authenticated users to write to their profile
|
||||
// allow only authenticated users to write to their profile
|
||||
server.exposeWritableRecord(/^profile:user:\d+$/, async (conn, recordId) => {
|
||||
const meta = await server.connectionManager.getMetadata(conn);
|
||||
const recordUserId = recordId.split(":").pop();
|
||||
return meta?.userId === recordUserId; // Check if user ID matches record ID
|
||||
return meta?.userId === recordUserId; // check if user ID matches record ID
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
@ -452,7 +452,7 @@ export class MeshClient extends EventEmitter {
|
||||
* @returns {Promise<{ success: boolean; history: string[] }>} A promise that resolves with the subscription result,
|
||||
* including a success flag and an array of historical messages.
|
||||
*/
|
||||
subscribe(
|
||||
subscribeChannel(
|
||||
channel: string,
|
||||
callback: (message: string) => void | Promise<void>,
|
||||
options?: { historyLimit?: number }
|
||||
@ -490,7 +490,7 @@ export class MeshClient extends EventEmitter {
|
||||
* @param {string} channel - The name of the channel to unsubscribe from.
|
||||
* @returns {Promise<boolean>} A promise that resolves to true if the unsubscription is successful, or false otherwise.
|
||||
*/
|
||||
unsubscribe(channel: string): Promise<boolean> {
|
||||
unsubscribeChannel(channel: string): Promise<boolean> {
|
||||
return this.command("unsubscribe-channel", { channel });
|
||||
}
|
||||
|
||||
|
||||
@ -50,7 +50,7 @@ describe("Redis Channel Subscription", () => {
|
||||
test("client can subscribe to a Redis channel", async () => {
|
||||
await client1.connect();
|
||||
|
||||
const result = await client1.subscribe("test:channel", () => {});
|
||||
const result = await client1.subscribeChannel("test:channel", () => {});
|
||||
expect(result.success).toBe(true);
|
||||
expect(Array.isArray(result.history)).toBe(true);
|
||||
});
|
||||
@ -58,7 +58,7 @@ describe("Redis Channel Subscription", () => {
|
||||
test("client cannot subscribe to an unexposed channel", async () => {
|
||||
await client1.connect();
|
||||
|
||||
const result = await client1.subscribe("unexposed:channel", () => {});
|
||||
const result = await client1.subscribeChannel("unexposed:channel", () => {});
|
||||
expect(result.success).toBe(false);
|
||||
expect(Array.isArray(result.history)).toBe(true);
|
||||
expect(result.history.length).toBe(0);
|
||||
@ -69,7 +69,7 @@ describe("Redis Channel Subscription", () => {
|
||||
|
||||
let receivedMessage: string | null = null;
|
||||
|
||||
await client1.subscribe("test:channel", (message) => {
|
||||
await client1.subscribeChannel("test:channel", (message) => {
|
||||
receivedMessage = message;
|
||||
});
|
||||
|
||||
@ -98,7 +98,7 @@ describe("Redis Channel Subscription", () => {
|
||||
|
||||
let messageCount = 0;
|
||||
|
||||
await client1.subscribe("test:channel", () => {
|
||||
await client1.subscribeChannel("test:channel", () => {
|
||||
messageCount++;
|
||||
});
|
||||
|
||||
@ -108,7 +108,7 @@ describe("Redis Channel Subscription", () => {
|
||||
setTimeout(resolve, 100);
|
||||
});
|
||||
|
||||
const unsubResult = await client1.unsubscribe("test:channel");
|
||||
const unsubResult = await client1.unsubscribeChannel("test:channel");
|
||||
expect(unsubResult).toBe(true);
|
||||
|
||||
await server.publishToChannel("test:channel", "Message 2");
|
||||
@ -127,11 +127,11 @@ describe("Redis Channel Subscription", () => {
|
||||
let client1Received: string | null = null;
|
||||
let client2Received: string | null = null;
|
||||
|
||||
await client1.subscribe("test:channel", (message) => {
|
||||
await client1.subscribeChannel("test:channel", (message) => {
|
||||
client1Received = message;
|
||||
});
|
||||
|
||||
await client2.subscribe("test:channel", (message) => {
|
||||
await client2.subscribeChannel("test:channel", (message) => {
|
||||
client2Received = message;
|
||||
});
|
||||
|
||||
@ -162,11 +162,11 @@ describe("Redis Channel Subscription", () => {
|
||||
const channel1Messages: string[] = [];
|
||||
const channel2Messages: string[] = [];
|
||||
|
||||
await client1.subscribe("test:channel", (message) => {
|
||||
await client1.subscribeChannel("test:channel", (message) => {
|
||||
channel1Messages.push(message);
|
||||
});
|
||||
|
||||
await client1.subscribe("test:channel2", (message) => {
|
||||
await client1.subscribeChannel("test:channel2", (message) => {
|
||||
channel2Messages.push(message);
|
||||
});
|
||||
|
||||
@ -187,7 +187,7 @@ describe("Redis Channel Subscription", () => {
|
||||
test("unsubscribing from a non-subscribed channel returns false", async () => {
|
||||
await client1.connect();
|
||||
|
||||
const result = await client1.unsubscribe("not:subscribed");
|
||||
const result = await client1.unsubscribeChannel("not:subscribed");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
@ -206,8 +206,8 @@ describe("Redis Channel Subscription", () => {
|
||||
(connection, channel) => connection.id === connection1.id
|
||||
);
|
||||
|
||||
const result1 = await client1.subscribe("guarded:channel", () => {});
|
||||
const result2 = await client2.subscribe("guarded:channel", () => {});
|
||||
const result1 = await client1.subscribeChannel("guarded:channel", () => {});
|
||||
const result2 = await client2.subscribeChannel("guarded:channel", () => {});
|
||||
|
||||
expect(result1.success).toBe(true);
|
||||
expect(result2.success).toBe(false);
|
||||
@ -223,7 +223,7 @@ describe("Redis Channel Subscription", () => {
|
||||
return true;
|
||||
});
|
||||
|
||||
await client1.subscribe("test:channel", () => {});
|
||||
await client1.subscribeChannel("test:channel", () => {});
|
||||
|
||||
expect(receivedChannel).toBe("test:channel");
|
||||
|
||||
@ -234,7 +234,7 @@ describe("Redis Channel Subscription", () => {
|
||||
return true;
|
||||
});
|
||||
|
||||
await client1.subscribe("test:channel:1", () => {});
|
||||
await client1.subscribeChannel("test:channel:1", () => {});
|
||||
|
||||
expect(receivedChannel).toBe("test:channel:1");
|
||||
});
|
||||
@ -251,7 +251,7 @@ describe("Redis Channel Subscription", () => {
|
||||
|
||||
const receivedMessages: string[] = [];
|
||||
|
||||
const { success, history } = await client1.subscribe("test:channel", (message) => {
|
||||
const { success, history } = await client1.subscribeChannel("test:channel", (message) => {
|
||||
receivedMessages.push(message);
|
||||
}, { historyLimit: 3 });
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user