@baasix/sdk 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -5
- package/dist/index.cjs +25 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -8
- package/dist/index.d.ts +24 -8
- package/dist/index.js +25 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1058,12 +1058,17 @@ channel.unsubscribe();
|
|
|
1058
1058
|
Custom rooms enable real-time communication between users for chat, games, or collaborative features. The **first user to join** a room becomes its creator. If the creator leaves temporarily, ownership transfers to the next member — but the **original creator automatically reclaims ownership** when they rejoin. If the room empties and is recreated, the next joiner becomes the new owner.
|
|
1059
1059
|
|
|
1060
1060
|
```typescript
|
|
1061
|
-
// Join a room
|
|
1062
|
-
await baasix.realtime.joinRoom('game:lobby'
|
|
1061
|
+
// Join a room — pass optional metadata stored alongside your membership
|
|
1062
|
+
await baasix.realtime.joinRoom('game:lobby', {
|
|
1063
|
+
username: 'Alice',
|
|
1064
|
+
avatar: 'https://example.com/alice.png',
|
|
1065
|
+
team: 'blue',
|
|
1066
|
+
});
|
|
1063
1067
|
|
|
1064
1068
|
// Get current members (you must be in the room)
|
|
1069
|
+
// Each entry includes userId, socketId, isCreator, and metadata
|
|
1065
1070
|
const members = await baasix.realtime.getRoomMembers('game:lobby');
|
|
1066
|
-
// [{ socketId: string, userId: string|number, isCreator: boolean }, ...]
|
|
1071
|
+
// [{ socketId: string, userId: string|number, isCreator: boolean, metadata: Record<string,any> }, ...]
|
|
1067
1072
|
|
|
1068
1073
|
// Send a message to all room members
|
|
1069
1074
|
await baasix.realtime.sendToRoom('game:lobby', 'chat', { text: 'Hello!' });
|
|
@@ -1073,9 +1078,9 @@ const unsubscribe = baasix.realtime.onRoomMessage('game:lobby', 'chat', (data) =
|
|
|
1073
1078
|
console.log(`${data.sender.userId}: ${data.payload.text}`);
|
|
1074
1079
|
});
|
|
1075
1080
|
|
|
1076
|
-
// Listen for users joining / leaving
|
|
1081
|
+
// Listen for users joining / leaving (joined event includes their metadata)
|
|
1077
1082
|
baasix.realtime.onRoomUserJoined('game:lobby', (data) => {
|
|
1078
|
-
console.log(`${data.
|
|
1083
|
+
console.log(`${data.metadata.username} joined`);
|
|
1079
1084
|
});
|
|
1080
1085
|
baasix.realtime.onRoomUserLeft('game:lobby', (data) => {
|
|
1081
1086
|
console.log(`${data.userId} left`);
|
package/dist/index.cjs
CHANGED
|
@@ -3695,25 +3695,36 @@ var RealtimeModule = class {
|
|
|
3695
3695
|
// Custom Rooms API
|
|
3696
3696
|
// ===================
|
|
3697
3697
|
/**
|
|
3698
|
-
* Join a custom room for real-time communication
|
|
3699
|
-
*
|
|
3698
|
+
* Join a custom room for real-time communication.
|
|
3699
|
+
*
|
|
3700
|
+
* You can optionally attach metadata (e.g. display name, avatar, team) that
|
|
3701
|
+
* will be visible to all other members via {@link getRoomMembers} and in
|
|
3702
|
+
* `room:user:joined` events.
|
|
3703
|
+
*
|
|
3704
|
+
* @param roomName - The room to join.
|
|
3705
|
+
* @param metadata - Optional key/value pairs stored alongside this member.
|
|
3706
|
+
*
|
|
3700
3707
|
* @example
|
|
3701
3708
|
* ```typescript
|
|
3702
|
-
* // Join a room
|
|
3703
|
-
* await baasix.realtime.joinRoom('game:lobby'
|
|
3704
|
-
*
|
|
3705
|
-
*
|
|
3706
|
-
*
|
|
3707
|
-
*
|
|
3709
|
+
* // Join a room with metadata
|
|
3710
|
+
* await baasix.realtime.joinRoom('game:lobby', {
|
|
3711
|
+
* username: 'Alice',
|
|
3712
|
+
* avatar: 'https://example.com/alice.png',
|
|
3713
|
+
* team: 'blue',
|
|
3714
|
+
* });
|
|
3715
|
+
*
|
|
3716
|
+
* // Listen for other users joining (includes their metadata)
|
|
3717
|
+
* baasix.realtime.onUserJoined('game:lobby', (data) => {
|
|
3718
|
+
* console.log(data.metadata.username, 'joined');
|
|
3708
3719
|
* });
|
|
3709
3720
|
* ```
|
|
3710
3721
|
*/
|
|
3711
|
-
async joinRoom(roomName) {
|
|
3722
|
+
async joinRoom(roomName, metadata = {}) {
|
|
3712
3723
|
if (!this.socket?.connected) {
|
|
3713
3724
|
throw new Error("Not connected. Call connect() first.");
|
|
3714
3725
|
}
|
|
3715
3726
|
return new Promise((resolve, reject) => {
|
|
3716
|
-
this.socket.emit("room:join", { room: roomName }, (response) => {
|
|
3727
|
+
this.socket.emit("room:join", { room: roomName, metadata }, (response) => {
|
|
3717
3728
|
if (response.status === "success") {
|
|
3718
3729
|
this.setupRoomListeners(roomName);
|
|
3719
3730
|
resolve();
|
|
@@ -3750,11 +3761,14 @@ var RealtimeModule = class {
|
|
|
3750
3761
|
* Get the list of users currently in a room.
|
|
3751
3762
|
* You must already be a member of the room to call this.
|
|
3752
3763
|
*
|
|
3764
|
+
* Each entry includes `userId`, `socketId`, `isCreator`, and `metadata`
|
|
3765
|
+
* (the custom object the member passed to {@link joinRoom}).
|
|
3766
|
+
*
|
|
3753
3767
|
* @example
|
|
3754
3768
|
* ```typescript
|
|
3755
3769
|
* const members = await baasix.realtime.getRoomMembers('game:lobby');
|
|
3756
3770
|
* members.forEach(m => {
|
|
3757
|
-
* console.log(m.
|
|
3771
|
+
* console.log(m.metadata.username, m.isCreator ? '(owner)' : '');
|
|
3758
3772
|
* });
|
|
3759
3773
|
* ```
|
|
3760
3774
|
*/
|