Broadcast - element-ts/lithium GitHub Wiki

A really helpful method on LiServer is broadcast(). With broadcast, you can invoke a command on every connected socket at the same time. It runs all commands in parallel but will wait for all sockets to respond (unless a socket times out). It then combines all the return values from all the sockets into a map where the key is the socket's id and the value is the return value for the command invoked on all of them.

Instead of using Promise.all(), this is using something else so that if a socket throws an error, it will not error out the entire broadcast. Instead, if a socket throws an error or cannot be reached, the broadcast manager will assign undefined as the return value for that socket.

For example, if you had a command named "getFavoriteNumber" where the parameter type is void and return value is number, the command registry would look like:

{
    ... // more commands

    getFavoriteNumber: {
        param: void,
        return: number
    }
}

You could send this broadcast to all clients like:

const returnValues = await server.broadcast("getFavoriteNumber", undefined);

where returnValues could look like:

{
    aaaa: 32,
    bbbb: undefined,
    cccc: 8
}

This can be really helpful if you need to query all the connected sockets but don't necessarily want to have to hold up the process and ask them one at a time when you already know it will all be the same type.