B4J Question More than one room in WebApp ChatRoom tutorial

LucaMs

Expert
Licensed User
Longtime User
If I'm not mistaken, in the WebApp Chatroom tutorial the chat room is represented by the ChatShared code module.
If you want to create more than one chat room, it is obviously necessary that this is a class; should the instances of this class be stored in a thread safe map declared and initialized in the Main?

What "precautions" (mostly related to threads and CallSubDelayed) / modifications would be necessary?


Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
should the instances of this class be stored in a thread safe map declared and initialized in the Main?
Yes. I think that it is a good design.

The map keys can be the room identifiers.

Note that the CallSubDelayed calls are important. Without them the code will be executed on the connection specific thread.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Note that the CallSubDelayed calls are important. Without them the code will be executed on the connection specific thread.
Yes but... is there a thread where the room code is executed? Of course it exists, but which is it?

Each instance of the websocket handler class (which I consider as a user, a client) has a reference to the room it "belongs to", so the code looks like:
B4X:
' websocket handler class
Private Room As clsRoom
Room = [one of the room in the Main.mapRooms]
CallSubDelayed2(Room, "RoutineName", "Data")

An user (websocket handler class) creates (declares and initializes) a new room, when needed, and add it to the Main.mapRooms.
Other clients will have a reference to that room, using the code above. In which thread will the room code run when called using CallSubDelayed from the clients that have a reference to that room?

The answer could be: "run a test". The point is that so far, since I had forgotten the difference between Debug and Release in this regard, I always started the server in Debug mode; when, by mistake, I did it in Release, I got very unpleasant results - even (or mainly?) a "Wait For", with the threads, ...! and then I remembered the difference between the two modes :(.

All very hard to explain using words only :(
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You should initialize the rooms from the main module. This way their code will be executed with the main thread (which is not used too much in server solutions).

"This channel allows us to build web apps where all (or most) of the logic is implemented in the server" [Erel].

I too would prefer that most of the logic and code were on the server but I thought that by running some of the code in the various threads the server would have had advantages.


Thanks, Erel.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This quote has nothing to do with my suggestion. Everything is implemented on the server side.
It has to do with the set of things to consider in order to decide if clients should be considered stupid data viewers or be them to consider the server as a stupid data provider, in my opinion.

The main reason why I want all (or almost) logic, code, control to be on the server is the security issue: it is always possible for someone to change a client.

Obviously it depends on how much processing is necessary to carry out "the task" but I fear that doing everything in the single main thread is too heavy for the server. It is as if the server were a client, with all the logic and, using only data as parameters provided by the clients, it worked as N clients belonging to a group and as M groups at the same time, instead (maybe) being able to exploit multi-threading the server could handle a greater number of clients.
 
Upvote 0
Top