Websocket Call in Java
-
@admin
Can someone please share a sample code on how to connect to websocket Angelone apis. I have tried connecting, sending a ping message and received a pong message. But when i tried with the below requests, i am not getting any reply from server. I would like to see a sample code so that i refer and create my own code.{ "correlationID": "abcde12345", "action": 1, "params": { "mode": 1, "tokenList": [ { "exchangeType": 1, "tokens": [ "14366" ] } ] } }
14366 is the token for IDEA-EQ stock. No response in both postman and through java.
Any help is much appreciated.
-
@admin
Thanks for replying 😀, I was finally able to parse the data properly and see the LTP dataByteBuffer packet = ByteBuffer.wrap(message.array()).order(ByteOrder.LITTLE_ENDIAN); ZonedDateTime exchangeTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(packet.getLong(35)), TZ_IST); System.out.print("\nToken Details : "+getTokenID(packet)); System.out.print("\nSequence Number: "+packet.getLong(27)); System.out.print("\nExchange TIme: "+exchangeTime); System.out.print("\nLTP: "+packet.getLong(43));
Output of the above code:
-
I wanted to share an example code in Java for working with Websocket 2.0 and the SmartStreamTicker class. This code can be used as a reference only and is not an official release. You can find the code on GitHub at https://github.com/angel-one/smartapi-java/pull/1/files
Please note that this code is provided as a sample only and is not an official release. It is not officially supported and should be used with caution.
This is just to give you an idea of the parsing logic and can be used as a reference only.
Please let me know if you have any questions or concerns.
-
@Ashok
i dont think its base64 encoding. in the documentation its mentioned that it is Little ENDIAN, but when i try to get the byte coding from the byteBuffer in Java it says BIG ENDIANThis is how the conversion goes(see screenshot below), i have tried all the available charset conversions but nothing is working.
and if i do message.flip().array(), where message is the byte buffer i receive from angel one, below is my result
[1, 1, 50, 56, 56, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -38, 37, 0, 0, 0, 0, 0, -24, 111, 51, 92, -123, 1, 0, 0, -71, -34, 3, 0, 0, 0, 0, 0]@admin Need ur help here, can someone show me how to parse the response from websocket server to a readable from in Java?? I am struck here for more than 2 weeks
-
@laskshmi I am not familiar with Java, but you need to parse the binary data. below is the reference function implemented by Zerodha. Should be similar.
Probably try base64 encoding ?
-
@laskshmi you need to read the response byte by byte as per this response contract in here - https://smartapi.angelbroking.com/docs/WebSocket2
Best ok luck !!
-
@admin
I have tried and able to successfully get the response back from webscoket server in both postman and in Java. but i am not able to read it.
REQUEST:{ "correlationID": "abcde12345", "action": 1, "params": { "mode": 1, "tokenList": [ { "exchangeType": 1, "tokens": [ "2885" ] } ] } }
RESPONSE:
Tried the same request in Java also, getting a ByteBuffer response object, but when i tried to decode it using different charset but still its in unreadble form.
message = java.nio.HeapByteBuffer[pos=0 lim=51 cap=51] UTF_8 = 2885 rK+ ?W? ]? UTF_16 = ??? r? ???]? ? UTF_16LE = ??? ?? ????? ? UTF_16BE = ??? r? ???]? ? US_ASCII = 2885 rK+ ?W? ]? ISO_8859_1 = 2885 rK+ �W? ]�
Response Screenshot from Intellij:
Please let me know how i can convert the response to a readable format. Thanks