0

I take data from Bittrex without WebSocket by this way

 request = WebRequest.Create("https://bittrex.com/api/v1.1/public/getticker?market=USDT-BTC");
                    request.Credentials = CredentialCache.DefaultCredentials;
                    response = (HttpWebResponse)request.GetResponse();
                    dataStream = response.GetResponseStream();
                    reader = new StreamReader(dataStream);
                    responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                    response.Close();

                    date = JsonConvert.DeserializeObject(responseFromServer);

It is very easy way and it is working on Bittrex. But I did a lot off request. I need do it on Bitfinex but I have the exeption "Too many request". As I understood I need WebSocket for this. By this adress https://api.bitfinex.com/v1/pubticker/BTCUSD. Somebody can show easy code to understand how I need to conect and write in Console info from WebSocket. Thanks!

FX_Sektor
  • 1,206
  • 2
  • 14
  • 32

1 Answers1

0

BIttrex release in March beta version of new site and WebSocket. GitHub repository have samples for usage WebSocket channel to subscribe for events.

Here is C# example:

using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using Microsoft.AspNet.SignalR.Client;

namespace WebsocketSample
{
    public sealed class BittrexWebsocket
    {
        public delegate void BittrexCallback(string info);

        private HubConnection   _hubConnection       { get; }
        private IHubProxy       _hubProxy            { get; }
        private BittrexCallback _updateExchangeState { get; }
        private BittrexCallback _updateOrderState    { get; }
        private BittrexCallback _updateBalanceState  { get; }

        public BittrexWebsocket(
            string          connectionUrl, 
            BittrexCallback updateExchangeState,
            BittrexCallback updateOrderState, 
            BittrexCallback updateBalanceState
            )
        {
            // Set delegates
            _updateExchangeState = updateExchangeState;
            _updateOrderState    = updateOrderState;
            _updateBalanceState  = updateBalanceState;

            // Create connection to c2 SignalR hub
            _hubConnection = new HubConnection(connectionUrl);
            _hubProxy      = _hubConnection.CreateHubProxy("c2");

            // Register callback for uE (exchange state delta) events
            _hubProxy.On(
                "uE", 
                exchangeStateDelta => _updateExchangeState?.Invoke(exchangeStateDelta)
                );

            // Register callback for uO (order status change) events
            _hubProxy.On(
                "uO", 
                orderStateDelta => _updateOrderState?.Invoke(orderStateDelta)
                );

            // Register callback for uB (balance status change) events
            _hubProxy.On(
                "uB", 
                balanceStateDelta => _updateBalanceState?.Invoke(balanceStateDelta)
                );

            _hubConnection.Start().Wait();
        }

        public void Shutdown() => _hubConnection.Stop();

        // marketName example: "BTC-LTC"
        public async Task<bool> SubscribeToExchangeDeltas(string marketName) => await _hubProxy.Invoke<bool>("SubscribeToExchangeDeltas", marketName);

        // The return of GetAuthContext is a challenge string. Call CreateSignature(apiSecret, challenge)
        // for the response to the challenge, and pass it to Authenticate().
        public async Task<string> GetAuthContext(string apiKey) => await _hubProxy.Invoke<string>("GetAuthContext", apiKey);

        public async Task<bool> Authenticate(string apiKey, string signedChallenge) => await _hubProxy.Invoke<bool>("Authenticate", apiKey, signedChallenge);

        // Decode converts Bittrex CoreHub2 socket wire protocol data into JSON.
        // Data goes from base64 encoded to gzip (byte[]) to minifed JSON.
        public static string Decode(string wireData)
        {
            // Step 1: Base64 decode the wire data into a gzip blob
            byte[] gzipData = Convert.FromBase64String(wireData);

            // Step 2: Decompress gzip blob into minified JSON
            using (var decompressedStream = new MemoryStream())
            using (var compressedStream = new MemoryStream(gzipData))
            using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
            {
                deflateStream.CopyTo(decompressedStream);
                decompressedStream.Position = 0;

                using (var streamReader = new StreamReader(decompressedStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }

        public static string CreateSignature(string apiSecret, string challenge)
        {
            // Get hash by using apiSecret as key, and challenge as data
            var hmacSha512 = new HMACSHA512(Encoding.ASCII.GetBytes(apiSecret));
            var hash = hmacSha512.ComputeHash(Encoding.ASCII.GetBytes(challenge));
            return BitConverter.ToString(hash).Replace("-", string.Empty);
        }
    }

    class Program
    {
        static public BittrexWebsocket.BittrexCallback CreateCallback(string name)
        {
            //
            // In a real app, your code would do something useful based on the
            // information accompanying each event.
            //

            return (info) => 
            {
                Console.WriteLine($"Callback Invoked: {name}");

                Console.WriteLine(
                    BittrexWebsocket.Decode(info)
                    );
            };
        }

        static void Main(string[] args)
        {
            Task task = Task.Run(
                async () => 
                {
                    string apiKey    = "YOUR_API_KEY";
                    string apiSecret = "YOUR_API_SECRET";
                    string baseUrl   = "https://beta.bittrex.com/signalr";

                    var btx = new BittrexWebsocket(
                        baseUrl, 
                        CreateCallback("exchange"), 
                        CreateCallback("order"), 
                        CreateCallback("balance")
                        );

                    // If we successfully authenticate, we'll be subscribed to the uO and uB events.
                    var isAuthenticated = await btx.Authenticate(
                        apiKey,
                        BittrexWebsocket.CreateSignature(apiSecret, await btx.GetAuthContext(apiKey))
                        );

                    // Register for orderbook updates on the BTC-ETH market
                    await btx.SubscribeToExchangeDeltas("BTC-ETH");
                });

            task.Wait();

            Console.WriteLine("Press enter to exit sample...");
            Console.ReadLine();
        }
    }
}

Run in PackageManager Console to add SignalR dependency:

Install-Package Microsoft.AspNet.SignalR.Client -Version 2.3.0

Also to connect you need get Key and Secret from your account on Bittrex.

            string apiKey    = "YOUR_API_KEY";
            string apiSecret = "YOUR_API_SECRET";

For Bitfinex you can try next code:

using System;
using WebSocketSharp;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ws = new WebSocket("wss://api.bitfinex.com/ws/2"))
            {
                ws.OnMessage += (sender, e) => Console.WriteLine(e.Data);

                ws.Connect();
                ws.Send("{\"event\":\"subscribe\", \"channel\":\"ticker\", \"pair\":\"BTCUSD\"}");
                Console.ReadKey(true);
            }
        }
    }
}

Requires dependency from Nuget:

Install-Package WebSocketSharp-NonPreRelease -Version 1.0.0
Nick Suslov
  • 124
  • 1
  • 12
  • Thanks, but I need it for Bitfinex – FX_Sektor Jul 27 '18 at 11:19
  • All other information about channels, events and pairs you can find in official bitfinex documentation [here](https://docs.bitfinex.com/v1/reference#ws-public-order-books). If answer is helpful for you please mark them as accepted. – Nick Suslov Jul 27 '18 at 12:16
  • You example work for bittrex. It is new step for me in WebSocket. But I need for beetfinex and for ticker. How I can rewrite your code for Bitfinex buy this adress wss://api.bitfinex.com/ws/2 – FX_Sektor Jul 27 '18 at 12:52
  • I already update answer, second part contains code for Bitfinex. Started from "For Bitfinex you can try next code:" – Nick Suslov Jul 27 '18 at 12:55