How can i use this with dart(flutter)?
-
is there any ref for how to use this api in dart(flutter)?
-
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';class ScreenAngelWebSocket extends StatefulWidget {
const ScreenAngelWebSocket({Key? key}) : super(key: key);@override
State<ScreenAngelWebSocket> createState() => _ScreenAngelWebSocketState();
}class _ScreenAngelWebSocketState extends State<ScreenAngelWebSocket> {
WebSocketChannel? channel;@override
void initState() {
super.initState();
channel = WebSocketChannel.connect(Uri.parse(ApiPath.WSS_ANGEL));
channel!.sink.add(
jsonEncode({
"task": "cn",
"channel": "",
"token": "feed_token",
"user": "client_id",
"acctid": "client_id"
}),
);channel!.sink.add(
jsonEncode({
"task": "mw",
// "channel": "nse_cm|3045",
"channel": "nse_cm|3045&nse_cm|2885", // SBI & Reliance
"token": "feed_token",
"user": "client_id",
"acctid": "client_id"
}),
);
channel!.stream.listen(
(data) {
String response = data.toString();
debugPrint('wss Response before Decode: ' + response);
var step1 = base64.decode(response);
debugPrint('wss Response Decode Step 1: ' + step1.toString());
var inflated = zlib.decode(step1);
var step2 = utf8.decode(inflated);
debugPrint('wss Response Decode Step 2: ' + step2.toString());var step3 = json.decode(step2);
debugPrint('wss Response Decode Step 3: ' + step3.toString());if (step3.toString().contains('task') &&
step3[0]['task'].toString() == 'cn' &&
step3[0]['msg'].toString() == 'cn') {
debugPrint('wss Socket Connected');
}
},
onDone: () {
debugPrint('wss Socket Connected');
},
onError: (error) => debugPrint('wss Error: ' + error.toString()),
);
}@override
void dispose() {
super.dispose();
channel!.sink.close();
}@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Web Socket Demo'),
],
);
}
}Decoded Data in Log
wss Response Decode Step 3: [{e: nse_cm, name: sf, ltp: 2597.25, ltq: 20, tk: 2885, ltt: NA}, {e: nse_cm, name: sf, ltp: 518.90, ltq: 8, tk: 3045, ltt: NA}]
Also find answer here https://smartapi.angelbroking.com/topic/1887/how-to-decode-web-socket-response-in-flutter-dart/2
-
@91priyansh I've done this (decoded successfully) check here https://smartapi.angelbroking.com/topic/1887/how-to-decode-web-socket-response-in-flutter-dart
-
Can anyone help me to decode response in flutter?
- topic:timeago_earlier,about a year
-
@James-Bond i am using web_socket_channel dart(package) to access websocket. here is my code
//in dart
void connectToWS() {
try {
channel = IOWebSocketChannel.connect(Uri.parse(
'wss://omnefeeds.angelbroking.com/NestHtml5Mobile/socket/stream'));channel.stream.listen((message) { print("Message $message"); }, onError: (error) { print(error); }); var reqJson = { "task": "cn", "channel": "", "token": "token", "user": "clientId", "acctid": "clientId" }; var req = jsonEncode(reqJson); channel.sink.add(req); // print("Sent connection request message"); } catch (e) { print(e); } //
}
-
@rajanprabu
No offence but if you closely look, you will get idea of why I'm creating multiple posts. I'm replying to particular post of questioner not creating double posts, because mostly on this forum, people who ask questions are either novice or they have no experience at all. So if I add everything in one post it confuse them further. -
@rajanprabu
Ok, I'll consider this next time.Earlier, sometimes this happened due to 3600 sec (1hr) limit. Sometimes I had to add more points later when it come to my mind, but this limit won't let me to do it.
-
You can also edit you posts and add more.. You can click on three dots and edit your post.
you are always making doubles posts within seconds. Please keep the threads uncluttered..
-
@91priyansh
Dart / Flutter is not officially supported. But you can use any officially supported SDK and further use it in your app. -
@91priyansh
How you accessing WebSocket ? By using official SDK's or by using pure WebSocket protocol and HTTP ?Also share your code here to get more help.
-
@admin i am getting data from like this eJyLrlZKzFayUsrPVtJRKkksBrGT84Ds3OJ0EDM/Ly81uSQ1Rak2FgAaGQ1z and eJyLjgUAARUAuQ==. Is there issue from client_side? if any one knows tell me solution?
-
@admin i am getting encoded data from web-socket. i am using dart(flutter). do you know y this is happening? if you have any ref for dart(flutter) it would be great..