B4J Question How can i send message to All connected users on ServerSocket ?

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Hi everyone !

How can i send message to "ALL" Connected Users by ServerSocket ?

Example :
B4X:
astream.writeToAll("MyMessageToAll")
 

OliverA

Expert
Licensed User
Longtime User
How can i send message to "ALL" Connected Users by ServerSocket ?
By keeping a list of clients that are connected and then looping through them to sent the message. For source of a server application using ServerSocket, see Erel's FTP server (https://www.b4x.com/android/forum/t...plemented-with-socket-and-asyncstreams.74320/). In the FTPServer class module, clients are tracked via the List variable clients.
I recommend you to use jServer with WebSockets
Even with WebSockets, you would have to track the connections an loop through the connections to sent the message. For an example on how this is accomplished, the the Chat example provided in the Web Apps Overview thread (https://www.b4x.com/android/forum/threads/webapp-web-apps-overview.39811/). For simplicity (needing no DB setup), download the ServerExampleNoMySQL.zip file.

Note: The FTP source is my fallback to look at code that uses ServerSocket for server applications and the Chat example is the my fallback to look at code for handling WebSocket communications. Neither may offer 100% what you are looking for, but I think they are a great starting point.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Even with WebSockets, you would have to track the connections an loop through the connections to sent the message
That's true, however it is still simpler then building a multi-client server with AsyncStream.

However the simplest solution for many to many communication is to use MQTT.
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Thanks you by your Messages !

Well , i am trying JServer with Websocket .

so when i call the function :

B4X:
ws.RunFunction("Time", Array As Object(DateTime.Time(DateTime.Now)))
           ws.Flush

Will send to all Connected users on this WebSocket ?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
No. Each ws object is a one to one relation between the server and a client.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top