B4J Question ABMaterial - Global variables

janderkan

Well-Known Member
Licensed User
Longtime User
Hi all

When I log in, my database module returns a map with user data.
I think of ABMaterial as a B4J app and put these data into a Process_Globals variable.
My friend think it is neccessary to put the user data in a session cookie.
Who is right ?


Also I would like to initialize a Mqtt connection after login.
Again I hold the Mqtt variable in a Process_Globals.
Is it possible ?

Jan
 

alwaysbusy

Expert
Licensed User
Longtime User
Safest is adding it to the session using SetAttribute(). Alternatively, you could put this data in a map in a module, BUT then this map must be created with 'srvr.CreateThreadSafeMap' (see ABMShared.CachedPages) AND you are then also responsible to clean it up when the user disconnects.

Adding it to the session is the cleanest way in my view.

As for the mqtt, can't you share the same mqtt connection and just subscribe to the correct channel depending on the user login?
 
Upvote 0

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
For the first part of your question, check the ABMFeedbak code (inlcuded in the ABMaterial package).
In the ABMApplication module, you will see how user data are stored after login, e.g.:
B4X:
ws.Session.SetAttribute("authType", "local")
ws.Session.SetAttribute("authName", inp1.Text)
ws.Session.SetAttribute("IsAuthorized", "true")
ws.Session.SetAttribute("UserType", "" & user.Get("usertype") )    ' lowercase!           
ws.Session.SetAttribute("UserID", "" & user.Get("userid") ) ' lowercase!



Also, in other modules of ABMFeedback, you will see how they are retrieved.
For example, in UsersPage module:

B4X:
If ABMShared.NeedsAuthorization Then
        If session.GetAttribute2("IsAuthorized", "") = "" Or page.ws.Session.GetAttribute2("UserType", "0") <> "1" Then
            ABMShared.NavigateToPage(ws, ABMPageId, "../")
            Return
        End If
End If

For your MQTT question, I am not knowledgeable enough to answer.

P.S. AlwaysBusy just outposted me!
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
As for the mqtt, can't you share the same mqtt connection and just subscribe to the correct channel depending on the user login?

Yes, but where to initialize the mqtt. Just in a module and init when the app is started?
Will this make the app connect when started and running , even when no one is online?
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Alternatively, you could put this data in a map in a module, BUT then this map must be created with 'srvr.CreateThreadSafeMap' (see ABMShared.CachedPages) AND you are then also responsible to clean it up when the user disconnects.
When first learning ABM, this option was not available. I too tried using a global map for user control. However, I soon discovered that all logged in users / devices were set to the LAST logged in user. @alwaysbusy then guided me to the proper approach using SetAttribute().

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
If I have only 1 connection to Mqtt and 10 users on the web app, this means that I must subscribe to all messages and evaluate in each user if the message is relevant ?
Please Start a new thread for this topic since this has nothing to do with the original (cookies / global map). One subject per thread is easier to follow...
We can help out there with this issue...
 
Upvote 0
Top