6

How do I consume data from, and push data to a websocket using Fable? I found this github issue which suggests that it can be done, but cannot find any documentation or examples of how to achieve this.

Lawrence
  • 3,287
  • 19
  • 32
  • 2
    I don't know the answer, but I know who does: the people that hang out on https://gitter.im/fable-compiler/Fable, including Fable's author, Enrico Sada. It's one of the most active Gitter channels I read, so you're pretty sure to get a quick response if you ask there. And if you get an answer to your question there, please come back and answer your own SO question so the answer can be Googled, because Gitter channels are hard for other people to Google later on. – rmunn Oct 24 '16 at 16:27
  • Maybe https://www.npmjs.com/package/fable-import-signalr. This might be a step up of course from "raw" websockets and as such not really what you want/need... – Helge Rene Urholm Oct 24 '16 at 16:37
  • 1
    Fable was created by Alfonso Garcia not Enrico Sada, both are awesome so easy to type the wrong name :) – Mr. Mr. Oct 25 '16 at 08:47
  • 1
    @Mr.Mr. - Thanks for correcting the record. I just realized my mistake and came back to edit my comment, but I'm no longer able to edit it. Thankfully, you'd already caught it and corrected the record. Yes, both of them have done awesome work on various parts of F#, but I didn't mean to deprive Alfonso Garcia-Caro of the credit for Fable. Sorry Alfonso! :-) – rmunn Oct 26 '16 at 04:50

1 Answers1

6

For anyone who finds this question later via Google, here's the response that @Lawrence received from Maxime Mangel when he asked this question on Gitter:

Hello @lawrencetaylor you can find here an old sample using websockets with FableArch. Don't consider the code 100% correct because it's from an older version of fable-arch.

This code should however show you how to use websockets with fable-arch logic. https://github.com/fable-compiler/fable-arch/commit/abe432881c701d2df65e864476bfa12cf7cf9343

First you create the websocket here.

Here you can see how to send a message over a websocket.

And here how to listen on a websocket.

I've copied the code he mentioned below, so that anyone who finds this question later will be able to read it without having to follow those links. Credit for the code below goes to Maxime Mangel, not me.

Websocket creation

let webSocket =
    WebSocket.Create("wss://echo.websocket.org")

Sending a message over a websocket

webSocket.send("Hello, socket!")

Listening on a websocket

let webSocketProducer push =
  webSocket.addEventListener_message(
    Func<_,_>(fun e ->
      push(ReceivedEcho (unbox e.data))
      null
  )
)

createApp Model.initial view update
|> withProducer webSocketProducer
|> start renderer

NOTE: ReceivedEcho in the above code is one of the cases of the Action discriminated union, which is a standard pattern in the fable-arch way of doing things. And withProducer is a function from fable-arch. See http://fable.io/fable-arch/samples/clock/index.html for a simple example of how to use withProducer.

rmunn
  • 34,942
  • 10
  • 74
  • 105
  • Thanks for posting this - I haven't had a chance to test the code out for myself yet, but surely will be useful. – Lawrence Oct 26 '16 at 06:56