Various ramblings-on, mostly about Red5

07 Apr 09 On-demand room / scope creation

I am not sure how or if FMS provides a means for dynamic rooms, but Red5 does and I will try to explain how to use it below. For the following example, you should expect the following url – rtmp://localhost/myapp/room is being used. The method below is an overridden super class method from the Red5 application adapter. The example places a client into an id based “chat” room; the id algorithm could be anything you desire and is not fixed. I have used this block in a couple projects, one of which used an encrypted room and id portion and its url looked similar to this – rtmp://localhost/myapp/29c8109abd29e

	public boolean roomJoin(IClient client, IScope scope) {
		log.debug("roomJoin - client id: {} scope: {}", client.getId(), scope);
		IConnection conn = Red5.getConnectionLocal();

		String scopeName = scope.getName();
		log.debug("Scope name: {}", scopeName);
		//check for "application" scope
		if ("myapp".equals(scopeName)) {
			log.debug("Connection already connected to app scope");
		} else if (conn.hasAttribute("")) {
			//client is already in a room
			log.debug("Connection already connected to room scope");
		} else {
			//the room name
			String roomName = scopeName;						
			//set flag
			conn.setAttribute("", true);
			//get app scope
			IScope appScope = scope.getParent();
			//handle at room level
			if ("room".equals(roomName)) {
				//lookup room (top level)
				IScope roomScope = ScopeUtils.resolveScope(appScope, roomName);
				if (roomScope == null) {
					if (appScope.createChildScope(roomName)) {
						log.debug("Room {} created", roomName);
						roomScope = appScope.getScope(roomName);
					} else {
						log.warn("Room {} was not created", roomName);
				} else {
					log.debug("Room scope {} was found", roomName);
				//get the next room identifier
				String chatId = getNextChatId();
				//room for chat id
				IScope chatScope = ScopeUtils.resolveScope(roomScope, chatId);
				if (chatScope == null) {
					if (roomScope.createChildScope(chatId)) {
						log.debug("Chat scope {} created", chatId);
						chatScope = roomScope.getScope(chatId);
					} else {
						log.warn("Chat scope {} was not created", chatId);
				} else {
					log.debug("Chat scope {} was found", chatId);
				//send the actual join
				return roomJoin(client, chatScope);
			} else {
				return false;
		return super.roomJoin(client, scope);

After this block is executed, your client would be connected to something like this – rtmp://localhost/myapp/room/1001 without having to create any additional NetConnections. The method “getNextChatId()” is meant to provide a chat room id, based on whatever criteria you would like.

Tags: , , , , , ,

Reader's Comments

  1. |

    With FMS you just connect to the server and add /”anything” to the connection string and another instance of the app will be created.



    this would create two separate instance of the app (rooms)

  2. |

    may i know why my scope.getName() and scope.getParent() is not working?
    is there any additional imports that i need to do besides, iScope, iBasicScope and ScopeUtils?

  3. |

    @bennie, yea but like Paul mentioned, that requires you to open/create multiple netconnections.

  4. |

    Hi Paul, thanks for your example.

    I tryed to use your code, but after the creation of the new room, che connection still remain on the first room.

    Do you have any ideas about why this is happening?


  5. |

    thanks for providing code. i would be thankful if someone provide me client side code too for this…..

  6. |

    Client code is simple a SharedObject connection. Check Adobe.

    Implemented this with FluorineFx and I have the same issue with Filippo, client is still connected on the first room.

    Any ideas?

    Filippo got a solution?

  7. |

    From what I’ve heard you can’t connect the client-side sharedObjects to the dynamically created room. What would I then use the above snippet for?

    More about the topic:–tp25853425p26244954.html

  8. |


    1) as stanley, I cannot get the scope.getParent() and scope.getName() working.
    So that makes that I cannot do anything at all…

    In my IScope interface, I only have getScopeNames function, that return a Iterator from which I can only get the next() object and not the current Name…

    What Can I do, then?

    2) In my logs, if I put: log.debug(“scope: ” + scope), I get:
    scope: [Scope@1bfefb Depth = 2, Path = ‘/default/TestJedai1’, Name = ‘laNouvellRoom’]…
    How can I get the laNouvellRoom info?

    Thank you.


  9. |

    Hello Paul

    Will this code work on 0.9? will the “roomJoin” method override the “connect” method when connecting via RTMP ?


  10. |

    Your code doesn’t work and you don’t answer the blog comments.
    Why is this trash still published?

  11. |

    Wow, thats the way you get help.. by being rude?

  12. |

    Hi Paul,

    thanks for sharing that! Scope management is a key concept in red5

    @paulo:I invite you to come and browse messages in the red5 google group… It’s a better place if you want some red5 related help.


  13. |

    I tested this. It’s true that the above code does not actually change the current scope of the client. Is there no other way?

  14. |

    To change the scope on the client would most likely require that a 302 be sent from the server. This code was an experiment and its scope changes did not sync correctly with the client side.

Leave a Comment

Fatal error: Call to undefined function akismet_counter() in C:\xampp\htdocs\paulgregoireblog\wp-content\themes\googlechrome\footer.php on line 9