delayed room variables update after creation of a new room

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

Post Reply
MMF
Posts: 6
Joined: 25 Mar 2024, 14:03

delayed room variables update after creation of a new room

Post by MMF »

I have created from the client side a new room with a CreateRoomRequest including room variables within the passed settings. When receiving the ROOM_ADD event the room variables are not immediately accessible by getVariables() for the client who had created the room. That is: getVariables() returns an empty list (or null or undefined; I cannot easily distinguish in the browser console). However, after a delay of a few seconds getVariables() in fact returns the right answer.

Do you consider this a bug? I assumed that upon receiving the success event (i.e. ROOM_ADD) all room variables (that were passed together with the other room settings) are already set correctly. Anyway, what principled way do you suggest to deal with the delay problem (as I need to access the room variables from some other part of the client program immediately after room creation)? Divide the requests into a CreateRoomRequest and an additional SetRoomVariablesRequest? After receiving the ROOM_VARIABLES_UPDATE event to a respective SetRoomVariablesRequest: Are the room variables then definitiely immediately accessible with the right values or is there still some safety delay for a client side access with getVariables() necessary? Thanks.
User avatar
Bax
Site Admin
Posts: 4626
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: delayed room variables update after creation of a new room

Post by Bax »

Room Variables are accessible only from inside the Room. That's why you can't read them when you receive the ROOM_ADD event.
Is there a chance that in what you call a delay, actually the client joined the Room, and it can access those variables as expected?
Paolo Bax
The SmartFoxServer Team
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: delayed room variables update after creation of a new room

Post by Lapo »

One small addition... RoomVars that have the "global" flag turned on can be accessed from Users not joined in the Room.
Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
MMF
Posts: 6
Joined: 25 Mar 2024, 14:03

Re: delayed room variables update after creation of a new room

Post by MMF »

I see. I combined the CreateRoomRequest with the autoJoin option but listened only on the ROOM_ADD event. Could it be that, in addition, I should wait on the ROOM_JOIN event?

Given that my current program logic cannot handle, without major modifications, to wait on two success events (here ROOM_ADD and ROOM_JOIN) rather than on a single one, is it safe to wait on ROOM_JOIN only (and catching the error events ROOM_CREATION_ERROR and ROOM_JOIN_ERROR) with the idea that once the ROOM_JOIN event is sent the respective ROOM_ADD event definitely has occurred before and in the error case I get at least a ROOM_CREATION_ERROR?

Anyway. Thanks for support.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: delayed room variables update after creation of a new room

Post by Lapo »

MMF wrote:I see. I combined the CreateRoomRequest with the autoJoin option but listened only on the ROOM_ADD event. Could it be that, in addition, I should wait on the ROOM_JOIN event?

It depends on what your client logic needs to do. If you need to access RoomVariables then you will need ROOM_JOIN, otherwise it's not necessary.

Given that my current program logic cannot handle, without major modifications, to wait on two success events (here ROOM_ADD and ROOM_JOIN) rather than on a single one, is it safe to wait on ROOM_JOIN only (and catching the error events ROOM_CREATION_ERROR and ROOM_JOIN_ERROR) with the idea that once the ROOM_JOIN event is sent the respective ROOM_ADD event definitely has occurred before and in the error case I get at least a ROOM_CREATION_ERROR?


Sure, this is definitely a valid way to do it.
Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
MMF
Posts: 6
Joined: 25 Mar 2024, 14:03

Re: delayed room variables update after creation of a new room

Post by MMF »

Lapo wrote:One small addition... RoomVars that have the "global" flag turned on can be accessed from Users not joined in the Room.
Cheers


A follow-up question: Where and how to set this global flag?
Please tell me if anything is wrong in my description: A client creates the room with room variables but cannot set the global flag for such a variable. Thus, once such a variable is created from the client side (together with creating the room) the flag must be set by the room extension (I have specified a JavaScript room extension). Within the room extension the init() function is the right place since the respective room variable should be visible to all users to which the room itself is visible right from the birth of this room.

The syntax in the JavaScript room extension for setting the global flag (given the room variable name is "room_var") now is:
getApi().getParentRoom().getVariable("room_var").isGlobal = true;

Is this correct? What worries me a bit is that my TypeScript compiler cannot find the "isGlobal"-property (and therefore marks this as an error) whether I import the class SFSRoomVariable from "sfs2x-api" or as SFSRoomVariable = SFS2X.Entities.Variables.SFSRoomVariable. I wish I could find some information within your documentation how to set up a JavaScript room/zone extension with TypeScript (i.e. how to configure correctly a bundler, say, webpack, where to put its configuration and where to put and how to configure tsconfig.json). That would be really nice. Thanks.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: delayed room variables update after creation of a new room

Post by Lapo »

A follow-up question: Where and how to set this global flag?

On the server side only.
You can create a Room Variable from server side and set the isGlobal flag. We don't provide this from client side because it can be easily abused to overload the server, as global variables generate a larger amount of updates since they can be visible by everyone.

More details here:
https://docs2x.smartfoxserver.com/Devel ... -variables
Thus, once such a variable is created from the client side (together with creating the room) the flag must be set by the room extension (I have specified a JavaScript room extension).

No flags are set when the RoomVariable is created and cannot be changed later.

As regards TypeScript, it's not supported on the server side. If you need a typed language you are much better off using Java.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply