Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the list of rooms? #16

Open
jbenguira opened this issue Feb 27, 2021 · 2 comments
Open

Get the list of rooms? #16

jbenguira opened this issue Feb 27, 2021 · 2 comments

Comments

@jbenguira
Copy link

jbenguira commented Feb 27, 2021

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 :)

@cedricfung
Copy link
Contributor

  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.

@paulm17
Copy link

paulm17 commented Dec 26, 2021

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants