kraken icon indicating copy to clipboard operation
kraken copied to clipboard

Get the list of rooms?

Open jbenguira opened this issue 3 years ago • 2 comments

Is it possible to get the list of rooms with the REST API? It would be great to have a JSON with the list of rooms + participants in each room

Something like this:

{"rooms": [
    {"name": "room1", users: 17},
    {"name": "room2", users: 5},
    {"name": "room3", users: 8},
    ...
]}

Also I noticed there is a quite long delay before a disconnected user is removed from the room, is this configurable?

Last point, the method "info" seems to be computed only once every 30 sec, is that also something that can be configured? if not could you give me a pointer about what part of the source code is responsible for this? :)

Thanks a lot for this great piece of OSS :)

jbenguira avatar Feb 27 '21 22:02 jbenguira

  1. No API to list rooms yet, and I think it's easy to add. But I feel that make the project complicated. We manage the rooms through a wrapper API to kraken. To add this API you can look at engine/router.go and engine/rpc.go, just add a method to respond the r.engine.rooms data.
  2. The quite long delay is a timeout mechanism, that's the final step to determine a user is disconnected. And for this purpose we also have a wrapper API to kraken, the wrapper will tell others that a user has left if they click the leave button. But if the user app crashes, then we can only learn the user leave by the timeout. But you can configure this timeout value from engine/peer.go, the peerTrackReadTimeout
  3. You can configure the value engineStateLoopPeriod at engine/engine.go.

Thanks for trying kraken. It's still too simple, but we have used it in production.

cedricfung avatar Feb 28 '21 03:02 cedricfung

@cedricfung Thanks so much for making this library!

@jbenguira It is absolutely possible to get all the rooms (that have been connected to) and connected users.

In router.go: above: https://github.com/MixinNetwork/kraken/blob/master/engine/router.go#L32

func (r *Router) listAll() (map[string][]string, error) {
	r.engine.rooms.RLock()

	rooms := make(map[string][]string)

	for _, pm := range r.engine.rooms.m {
		pm.RLock()
		for _, p := range pm.m {
			_, ok := rooms[p.rid]

			if !ok {
				rooms[p.rid] = make([]string, 0)
			}

			if p.pc.ConnectionState().String() == "connected" {
				rooms[p.rid] = append(rooms[p.rid], p.uid)
			}
		}
		pm.RUnlock()
	}
	r.engine.rooms.RUnlock()

	return rooms, nil
}

In rpc.go, above: https://github.com/MixinNetwork/kraken/blob/master/engine/rpc.go#L89

case "listAll":
    rooms, err := impl.listAll()
    if err != nil {
        renderer.RenderError(err)
    } else {
        renderer.RenderData(map[string]interface{}{"rooms": rooms})
    }

In rpc.go, above: https://github.com/MixinNetwork/kraken/blob/master/engine/rpc.go#L164

func (r *R) listAll() (map[string][]string, error) {
	return r.router.listAll()
}

How I use this. I already have a data layer of all the existing rooms. What I don't have, is whether any users exist in the rooms when a user first initialises the chat application. So requesting this endpoint gives me a snapshot at that time!

paulm17 avatar Dec 26 '21 11:12 paulm17