Giter VIP home page Giter VIP logo

hs-wss-binance's Introduction

hs-wss-binance

IMPORTANT NOTE: This is by no means production-grade or official library, it was built just for fun :)

It is a client for Binance web socket streaming API, described here. However, only listening to stream messages is implemented, not the sending of messages to server.

Client uses wuss library under the hood, which in turn uses websockets package.

Binance servers send PING messages from time to time, and if client does not respond in appropriate time, connection is closed. That part is being handled by websockets library automatically. But Binance will still close any connections that are open for more than 24h, that part needs to be handled by the user explicitly.

Common Usage

listenFor is kinda the entrypoint, as it gets the stream and returns Chan that can be used to listen for messages.

There are some predefined streams in appropriate modules, like tradeOf, allMarketBookTickerOf, etc.

Streams can also be combined, so a single connection can be used to listen for multiple messages, using world-peace open sum implementation. For example, one can do

main :: IO ()
main = do
  (priceInChan, priceOutChan) <- newChan 10
  concurrently_
    ( listenFor
        ( partialBookDepthOf (TradingPair @"adausdt") Five OneSecond
            `combineWith` differentialDepthOf (TradingPair @"bnbbtc") HundredMilliseconds
            `combineWith` tradeOf (TradingPair @"bnbbtc")
        )
        priceInChan
    )
    (readPrice priceOutChan)

readPrice :: Show a => OutChan a -> IO ()
readPrice channel = forever $ do
  msg <- readChan channel
  print msg

Resulting type that would be used in Chan-s would then look like

(RecievePayload (OpenUnion '[PartialBookDepthStreamResponse, TradeResponse, DifferentialDepthStreamResponse]))

Customizing responses

There is a little room for customization, if one does want to customize messages in any way - maybe we are not interested in all response fields, or there is some other reason.

If thats the case, one cam implement custom data type and define FromJSON and ToJSON instances, and create stream like so:

data Blabla = Blabla {
    blablaQuantity :: Float,
    blablaBuyerOrderId :: Integer
} deriving stock (Eq, Show, Generic)
  deriving anyclass (ToJSON)

instance FromJSON Blabla where
  parseJSON = withObject "Blabla" $ \v ->
    Blabla <$> fmap (read @Float) (v .: "q")
      <*> v .: "b"    

blablaTrade :: TradingPair cName -> StreamOf
     '[StreamType (AppendSymbol cName "@trade") Blabla] '[Blabla]
blablaTrade pair = streamOf @Blabla (TradingOf pair)

Listening to that stream would yield responses of type Blabla (again, wrapped in PayloadResponse and single element open sum)

hs-wss-binance's People

Contributors

nikola-maric avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.