B4J Question jServer and websocket Question ? How to get rid of {"data":[],"etype":"setAutomaticEvents"}

tufanv

Expert
Licensed User
Longtime User
Hello,

When conencting with a b4x or non b4x client to a websocket created by b4j with jserver, just after the client connects to server, he/she gets
{"data":[],"etype":"setAutomaticEvents"}

1)How can I make this dissapear ?
2) Clients are disconnected randomly between in 2 to15 minutes when connected to websocket with jserver. why so much disconnections? I also use websocket to connect to a data provider and never get disconnected for days maybe. What may be wrong?
Thank you.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Are you using WebSocketHandler: https://www.b4x.com/android/forum/threads/jwebsocketclient-library.40985/#content ?


This is strange. You should be able to stay connected for an unlimited time as long as the connection is active. Try to send a "ping" message every few seconds.
No, not using the wsh, I am using jwebsocketclient, clients will be customer's own methods so I am trying to test with every possible option. I will try to send a ping every few seconds and post my findings here. is ping message a special thing that must be sent or just a regulat message to send to server that may be ignored by the server ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Anything will work.

jServer WebSocket implementation is intended for working with browsers. You will need to build the client based on WebSocketHandler (which is quite simple).
Thanks for clarification. testing now, will post my findings here. BTW, I didnt know that it was meant for web browsers, I am using it for my business for years and didnt have a problem even 1 time :) but still i will change them to handler version.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Anything will work.

jServer WebSocket implementation is intended for working with browsers. You will need to build the client based on WebSocketHandler (which is quite simple).

I can't see a b4j websockethandler, I only find it for b4a? Am i missing it=?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I have found the problem but I dont know how to solve it. Websocket Server gets a data from the mysql server with mysql connector on the main sub and on the ws module it manages the websocket connections and serves this data. On the login procedure of websocket server, server also requests which data to send from the client. For example the currencies client wants to get price info with websocket like : EUR/USD, USD/JPY etc.. So, in the ws module I have this code:

B4X:
    Do While sayi<usersymbols.Size

        Do While Main.Cursor.NextRow
            
            If Main.Cursor.GetString("sembol")=usersymbols.Get(sayi) Then
'                Log(Main.Cursor.GetString("sembol"))
'                Log(usersymbols.Get(sayi))
                If data.Length=0 Then
                    data=data&$"[{"currency":"${Main.Cursor.Getstring("sembol")}","value":"${Main.Cursor.Getstring("last")}","change_percent":"${Main.Cursor.Getstring("changenumber")}","change":"${Main.Cursor.Getstring("changepercent")}"}"$
                Else
                    data=data&$",{"currency":"${Main.Cursor.Getstring("sembol")}","value":"${Main.Cursor.Getstring("last")}","change_percent":"${Main.Cursor.Getstring("changenumber")}","change":"${Main.Cursor.Getstring("changepercent")}"}"$
                End If
            End If
            Exit
        Loop
        sayi=sayi+1
    Loop

and later "data" is sent to client.
If i diasble this code and send a "test" string, connection never brokes. So the problem is in this code, maybe when I am getting the requested currencies from the main sub's resultset problem occurs. Is there a better way to handle this ? ( the reason I loop in main's result set is, result set have 200 currencies but I only want to serve the client'^s requested currencies)

TY
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Websocket Server gets a data from the mysql server with mysql connector on the main sub and on the ws module it manages the websocket connections and serves this data
Please note that the Main.Cursor.NextRow call in one WebSocket connection affects ALL WebSocket connections. You either need to

a) Use pooling and do your SELECT in each WebSocket connection and you can use the usersymbols list to create a SQL statement (parameterized) to send to the SQL server. Then use the results to process your return string
b) In main, store the SQL results in a list and process this list in your WebSocket handlers (although we may have access contention issues at this point -> the list may get build as we try to access it from the websocket handler)
c) Store the results in a local SQLite DB (on disk or in memory) and query it in the WebSocket handler

If at all possible, I would use option a. Use option c if you think you can query the SQL server back end so often (you are limited in the number of requests you can send to it) or if the back end SQL server connection is slow.

Minor nitpick: Don't use & to concatenate strings in B4J, especially if you are building a longer string. Each & will create a new string (since strings are immutable in B4X). Use StringBuilder to build the strings. I've seen cases where & caused memory and timing issues (& is not only memory intensive, but processing intensive).
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thanks for the great advice. I will change to stringbuilder. I have changed the result set system to json with httputils, it seems to fixed the problem. As I am serving the same datqa to all customers, it is not a problem for me that data changes. I only pick the required ones from all the set while distrubitng it.

Thanks !
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Looking at the jServer source, no. This is something done during the initial WebSocket connection. Could put a WISH in for changing jServer's WebSocket setup where it does not sent this if the events list is empty.

Code in jServer where setEvents is called during WebSocket setup: https://github.com/AnywhereSoftware...software/b4j/object/WebSocketModule.java#L203
Code in jServer that sends the JSON, even if the list (named events) is empty: https://github.com/AnywhereSoftware...ywheresoftware/b4j/object/WebSocket.java#L392
 
Upvote 0
Top