B4J Question Multiple classes with async streams communications.

hatzisn

Expert
Licensed User
Longtime User
Good afternoon to everyone,

I read that async stream is asynchronous. Let's say I implement in a class something similar to what is written in @Erel's tutorial here. Afterwards I create an array (with length N) of these classes (with everyone of them listening in a different port). If I have n<N computers that connect to them and try to send data with InitializePrefix (simultaneously from the n computers to the host with array) what will happen? Receive a chunk and then leave the thread open with a kind of (wait for) for the next allowing also the other n-1 to receive data or something else? I am asking this to save me time from experimenting.


Thanks in advance
 

hatzisn

Expert
Licensed User
Longtime User
It will work. Each connection / AsyncStream will work mostly independently. The NewData events are raised on the main thread.

With that said - I don't recommend building complex solutions with AsyncStreams unless you have no choice. Better to use MQTT or jServer.

You are right. I did not mention it. I am using a JServer with this array located in a background worker class. So according to what you said I can conclude that everything will run in the background worker thread. Right?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
This is an important point as jServer solutions have a different threading model.
Start here: https://www.b4x.com/android/forum/t...ads-sessions-and-server-events.39969/#content
Thanks for answering. This is an extremely interesting post. I have a question for this: If we call the method f.e. ChatShared.Init without CallSubDelayed but directly from a background Worker thread, since ChatShared is a shared module, where does the code of "Init sub" run? In the main thread or in the worker thread?


You need to provide more information about your architecture. How are you creating the classes instances? How are you interacting with those instances?

Everything (all the functionality - f.e. the async_newdata sub, async_error etc.) runs in the async stream class. I just create a new class for the worker and initialize the async stream classes as an array in it. The worker has now also active code for managing the newdata events with callsub from the classes but I will move everything inside the async stream classes so do not take this under notice. What attitude do you think should I expect from this?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thanks for answering. This is an extremely interesting post. I have a question for this: If we call the method f.e. ChatShared.Init without CallSubDelayed but directly from a background Worker thread, since ChatShared is a shared module, where does the code of "Init sub" run? In the main thread or in the worker thread?
When you directly call a method, it always run on the same thread. It is only when you call it with CallSubDelayed that the call can be transferred to a different thread.

If all class instances are created from the worker class instance then they will be "owned" by the worker thread.

Note that you can always verify it by logging the value of Main.srvr.CurrentThreadIndex.
And make sure to test it in release mode as in debug mode everything runs on the main thread.
 
Upvote 0
Top