B4J Question websocket receive data

tufanv

Expert
Licensed User
Longtime User
Hello,

I am trying to use websockets to connect to a socket with a wss url to get live ticker updates on a currency pair.

the documentation says :
  • The base endpoint is: wss://stream.binance.com:9443
  • Streams can be access either in a single raw stream or a combined stream
  • Raw streams are accessed at /ws/<streamName>
  • Combined streams are accessed at /stream?streams=<streamName1>/<streamName2>/<streamName3>
  • Combined stream events are wrapped as follows: {"stream":"<streamName>","data":<rawPayload>}
  • All symbols for streams are lowercase
  • A single connection to stream.binance.com is only valid for 24 hours; expect to be disconnected at the 24 hour mark

Individual Symbol Ticker Streams
24hr Ticker statistics for a single symbol pushed every second

Stream Name: <symbol>@ticker

Payload:
So I used :

B4X:
    ws.Initialize("ws")
    ws.Connect("wss://stream.binance.com:9443/ws/BNBBTC@ticker")

I am getting ws_connected on the logs but I can't find out how can i get the pushed stream.

documentation says I will get stg like this:

B4X:
{
  "e": "24hrTicker",  // Event type
  "E": 123456789,     // Event time
  "s": "BNBBTC",      // Symbol
  "p": "0.0015",      // Price change
  "P": "250.00",      // Price change percent
  "w": "0.0018",      // Weighted average price
  "x": "0.0009",      // Previous day's close price
  "c": "0.0025",      // Current day's close price
  "Q": "10",          // Close trade's quantity
  "b": "0.0024",      // Best bid price
  "B": "10",          // Bid bid quantity
  "a": "0.0026",      // Best ask price
  "A": "100",         // Best ask quantity
  "o": "0.0010",      // Open price
  "h": "0.0025",      // High price
  "l": "0.0010",      // Low price
  "v": "10000",       // Total traded base asset volume
  "q": "18",          // Total traded quote asset volume
  "O": 0,             // Statistics open time
  "C": 86400000,      // Statistics close time
  "F": 0,             // First trade ID
  "L": 18150,         // Last trade Id
  "n": 18151          // Total number of trades
}

it is the first time i am working with websockets, I tried checking out this topic : https://www.b4x.com/android/forum/threads/jwebsocketclient-library.40985/

but the example client has 2 modules which i am not sure if i have to use or not. Can you point me to right direction to work.

Thanks
 

OliverA

Expert
Licensed User
Longtime User
Do you have your ws_TextMessage event sub setup? it should receive a string (the message sent to you by the server).
B4X:
Private Sub ws_TextMessage(someText as String)
End Sub
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Do you have your ws_TextMessage event sub setup? it should receive a string (the message sent to you by the server).
B4X:
Private Sub ws_TextMessage(someText as String)
End Sub
I had answered this question but obviosuly I did not hit the send button. Nothing is received under this sub when I put this in the code.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You shouldn't use WebSocketHandler module as it is intended for communication with B4J WebSocket servers.

If ws_TextMessage is not raised then the server didn't send any data.
after further investigation I found out that , lowercase was needed for pairs that's why I wasnt getting any data. But now I am getting :

B4X:
org.eclipse.jetty.websocket.api.MessageTooLargeException: Text message size [69727] exceeds maximum size [65536]

the error is obvious. What can be done to this problem ?
 
Upvote 0
Top