Skip to content

Commit

Permalink
[IMP] add broadcast arbitrary messages
Browse files Browse the repository at this point in the history
This feature allows the broadcasting of arbitrary messages between
clients.

task-4047972
  • Loading branch information
Goaman authored and ThanhDodeurOdoo committed Jul 11, 2024
1 parent fabca39 commit 4bc51b4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ const sfu = new SfuClient();
sfu.disconnect();
sfu.state === SFU_CLIENT_STATE.DISCONNECTED; // true
```
- broadcast()
```js
// in the sender's client
sfu.broadcast("hello");
```
```js
// in the clients of other members of that channel
sfu.addEventListener("update", ({ detail: { name, payload } }) => {
switch (name) {
case "broadcast":
{
const { senderId, message } = payload;
console.log(`${senderId} says: "${message}"`); // 87 says "hello"
}
return;
// ...
}
});
```
- updateUpload()
```js
const audioStream = await window.navigator.mediaDevices.getUserMedia({
Expand Down
18 changes: 17 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ export class SfuClient extends EventTarget {
return this._state;
}

/**
* @param message any JSON serializable object
*/
broadcast(message) {
this._bus.send(
{
name: CLIENT_MESSAGE.BROADCAST,
payload: message,
},
{ batch: true }
);
}

/**
* @param {string} url
* @param {string} jsonWebToken
Expand Down Expand Up @@ -508,7 +521,7 @@ export class SfuClient extends EventTarget {
/**
* dispatches an event, intended for the client
*
* @param { "disconnect" | "info_change" | "track" | "error"} name
* @param { "disconnect" | "info_change" | "track" | "error" | "broadcast"} name
* @param [payload]
* @fires SfuClient#update
*/
Expand Down Expand Up @@ -557,6 +570,9 @@ export class SfuClient extends EventTarget {
*/
async _handleMessage({ name, payload }) {
switch (name) {
case SERVER_MESSAGE.BROADCAST:
this._updateClient("broadcast", payload);
break;
case SERVER_MESSAGE.SESSION_LEAVE:
{
const { sessionId } = payload;
Expand Down
11 changes: 11 additions & 0 deletions src/models/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,17 @@ export class Session extends EventEmitter {
*/
async _handleMessage({ name, payload }) {
switch (name) {
case CLIENT_MESSAGE.BROADCAST:
{
this._broadcast({
name: SERVER_MESSAGE.BROADCAST,
payload: {
senderId: this.id,
message: payload,
},
});
}
break;
case CLIENT_MESSAGE.CONSUMPTION_CHANGE:
{
/** @type {{ sessionId: number, states: Object<boolean> }} */
Expand Down
4 changes: 4 additions & 0 deletions src/shared/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const SERVER_REQUEST = {
};

export const SERVER_MESSAGE = {
/** Signals that the server wants to send a message to all the other members of that channel */
BROADCAST: "BROADCAST",
/** Signals the clients that one of the session in their channel has left. */
SESSION_LEAVE: "SESSION_LEAVE",
/** Signals the clients that the info (talking, mute,...) of one of the session in their channel has changed. */
Expand All @@ -38,6 +40,8 @@ export const CLIENT_REQUEST = {
};

export const CLIENT_MESSAGE = {
/** Signals that the client wants to send a message to all the other members of that channel */
BROADCAST: "BROADCAST",
/** Signals that the client wants to change how it consumes a track (like pausing or ending the download) */
CONSUMPTION_CHANGE: "CONSUMPTION_CHANGE",
/** Signals that the info (talking, mute,...) of this client has changed. */
Expand Down
17 changes: 17 additions & 0 deletions tests/network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,21 @@ describe("Full network", () => {
const [closeEvent] = await closeProm;
expect(closeEvent.code).toBe(SESSION_CLOSE_CODE.P_TIMEOUT);
});
test("A client can broadcast arbitrary messages to other clients on a channel that does not have webRTC", async () => {
const channelUUID = await network.getChannelUUID(false);
const user1 = await network.connect(channelUUID, 1);
const user2 = await network.connect(channelUUID, 2);
const sender = await network.connect(channelUUID, 3);
const message = "hello";
sender.sfuClient.broadcast(message);
const prom1 = once(user1.sfuClient, "update");
const prom2 = once(user2.sfuClient, "update");
const [[event1], [event2]] = await Promise.all([prom1, prom2]);
expect(event1.detail.name).toEqual("broadcast");
expect(event2.detail.name).toEqual("broadcast");
expect(event1.detail.payload.senderId).toBe(sender.session.id);
expect(event2.detail.payload.senderId).toBe(sender.session.id);
expect(event1.detail.payload.message).toBe(message);
expect(event2.detail.payload.message).toBe(message);
});
});

0 comments on commit 4bc51b4

Please sign in to comment.