Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active February 14, 2023 18:09
Show Gist options
  • Save Utopiah/8bf9375ef73d46b6c471c3ec2abae405 to your computer and use it in GitHub Desktop.
Save Utopiah/8bf9375ef73d46b6c471c3ec2abae405 to your computer and use it in GitHub Desktop.
// Get the URL of a Hubs room that is not empty nor at room capacity
// Warning: the proper API is being defined at https://github.com/mozilla/reticulum/pull/382
let getBestRoom = async () => {
// by https://twitter.com/jamesckane for @paracreative worked on the #ApartPosters show.
// let endpoint = "https://show.apartposters.com/api/v1/media/search?source=rooms&filter=public";
let endpoint = "/api/v1/media/search?source=rooms&filter=public";
let response = await fetch(endpoint);
let json = await response.json();
let rooms = json.entries;
let bestRoom = {};
rooms.forEach(room => {
if (Object.keys(bestRoom).length === 0) bestRoom = room;
let memberCount = room.member_count;
let lobbyCount = room.lobby_count;
let totalCount = memberCount + lobbyCount;
let buffer = 5;
let roomCapacity = room.room_size - buffer;
if ((totalCount >= 0 && totalCount < roomCapacity)) {
bestRoom = room;
}
});
return bestRoom.url;
// window.location.href = bestRoom.url; // to directly redirect and balance your server load
};
// PS: See also https://gist.github.com/camelgod/a712ce7d43f21260ae5b4971b9a58dfb
// to limit to a set of rooms via ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment