Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs
    1. Home
    2. civi
    C
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 7
    • Best 0
    • Groups 0

    Ketan Ramani

    @civi

    0
    Reputation
    1
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online
    Website stackoverflow.com/users/6667442/ketan-ramani Location Rajkot

    civi Unfollow Follow

    Latest posts made by civi

    • RE: How can i use this with 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

      posted in General Discussion
      C
      civi
    • RE: websocket gives wrong format response in flutter

      @rakeshbharati Check here https://smartapi.angelbroking.com/topic/1887/how-to-decode-web-socket-response-in-flutter-dart

      posted in General Discussion
      C
      civi
    • RE: How can i use this with dart(flutter)?

      @91priyansh I've done this (decoded successfully) check here https://smartapi.angelbroking.com/topic/1887/how-to-decode-web-socket-response-in-flutter-dart

      posted in General Discussion
      C
      civi
    • RE: how to decode web socket response in flutter / dart

      Replace initState block in above example. This is working

      @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",
      "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()),
      

      );
      }

      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}]

      posted in General Discussion
      C
      civi
    • how to decode web socket response in flutter / dart

      import 'dart:convert';
      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": "your_token",
      "user": "clientId",
      "acctid": "clientId"
      }),
      );
      // channel!.sink.add(
      // jsonEncode({
      // "task": "mw",
      // "channel": "bse_cm|532921",
      // "token": "your_token",
      // "user": "clientId",
      // "acctid": "clientId"
      // }),
      // );
      channel!.stream.listen(
      (data) {
      String response = data.toString();
      debugPrint('wss Response before Decode: ' + response);
      // String resToDecode = response.split('.')[0];
      // List<int> res = base64.decode(base64.normalize(resToDecode));
      // debugPrint('wss Response after Decode: ' + utf8.decode(res));
      // String decodedText = utf8.decode(base64.decode(response));
      // debugPrint('wss Response after Decode: ' + decodedText);
      },
      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'),
      ],
      );
      }
      }

      I'm using web_socket_channel: ^2.1.0 library for WebSocket and I'm also able to get the response from WebSocket. But I'm not able to decode response in flutter

      I have referred available SDKs provided by angelbroking and I can see we need to follow some steps to get that response in JSON format.

      byte[] decoded = Base64.getDecoder().decode(message); // By decoding using base64 will gives byte array
      byte[] result = decompress(decoded); // By decompressing again will gives byte array
      String str = new String(result, StandardCharsets.UTF_8); // By converting UTF_8 will gives JSON response in String

      Angel Broking SDKs

      https://github.com/angelbroking-github/smartapi-java/blob/dd93400a43387b6d3d93803a2b76d9d24bada42e/src/main/java/com/angelbroking/smartapi/smartTicker/SmartWebsocket.java#L138

      https://github.com/angelbroking-github/smartapi-dotnet/blob/5cd0ce09b1bd9c558e96941aa8b1edd691778cc6/AngelBroking/AngelBroking/WebSocket.cs#L68
      https://github.com/angelbroking-github/smartapi-dotnet/blob/5cd0ce09b1bd9c558e96941aa8b1edd691778cc6/AngelBroking/AngelBroking/Helpers.cs#L14

      https://github.com/angelbroking-github/smartapi-php/blob/b04ac7296b04d2b1527937dd009afb5b64e0975e/src/socket.js#L43

      https://github.com/angelbroking-github/smartapi-javascript/blob/6cbbac9ddb37996964401830d02154aa1275d682/lib/websocket.js#L36

      https://github.com/angelbroking-github/smartapigo/blob/52b461a8be7a8896e4d74ee20f1a60f3b17e526a/websocket/websocket.go#L193

      https://github.com/angelbroking-github/smartapi-r/blob/b24fa87b88f787d4aeae75eef967beec5b32f39c/R/websocket.R#L56

      posted in General Discussion
      C
      civi
    • RE: How can i use this with dart(flutter)?

      Can anyone help me to decode response in flutter?

      posted in General Discussion
      C
      civi
    • RE: websocket gives wrong format response in flutter

      @rakeshbharati Response format is not working. The response is in encoded format. We need to decode it. I'm also looking for a way to decode it. But I didn't get any way to decode it in flutter.

      Check below thread for similar issue. https://smartapi.angelbroking.com/topic/472/how-can-i-use-this-with-dart-flutter

      posted in General Discussion
      C
      civi