<div dir="ltr">Hello,<div><br></div><div>I've been trying to use the WebSockets library to connect to the coinbase feed. The library seems to be doing something behind the scenes that I'm not expecting.</div><div><br></div><div>Here is my code:</div><div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">{-# LANGUAGE ViewPatterns #-}<br>{-# LANGUAGE OverloadedStrings #-}<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">import Data.Text<br>import Data.Text.Encoding (decodeUtf8)<br>import Network.WebSockets<br>import qualified Data.ByteString.Lazy as LBS<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">main :: IO ()<br>main = runClient "<a href="http://ws-feed.exchange.coinbase.com">ws-feed.exchange.coinbase.com</a>" 8080 "/" handleConnection<br>handleConnection connection = do<br> send connection initSub<br> let loop = do<br> priceMsg <- receiveDataMessage connection<br> print priceMsg<br> loop<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">initSub :: Message<br>initSub = DataMessage $ Text "{\"type\":\"subscribe\", \"product_id\":\"BTC-USD\"}"</blockquote></div><div><br></div><div>When I run this, I get a print out showing a malformed response exception with a Moved permanently message. Now, it also shows the location that I was trying to connect to, which is: "<a href="https://ws-feed.exchange.coinbase.com/">https://ws-feed.exchange.coinbase.com/</a>". </div><div><br></div><div>This isn't right, because the websocket feed uses the wss:// protocol rather than https://. i've tried changing the url to be "wss://<a href="http://ws-feed.exchange.coinbase.com/">ws-feed.exchange.coinbase.com/</a>", but when I try that, dns resolution fails. If I had to guess, I could imagine that it tries to look for "https://wss://<a href="http://ws-feed.exchange.coinbase.com">ws-feed.exchange.coinbase.com</a>", which would obviously not work. I'm wondering if there is a way within the library to change the protocol that client uses. If there is, it doesn't seem to be documented. </div><div><br></div><div>Any insights as to what's going on here would be incredibly helpful.</div><div><br></div><div>Thanks,</div><div>Noah</div>
</div>