Giter Club home page Giter Club logo

elm-websocket-client's Introduction

WebSockets for Elm 0.19

billstclair/elm-websocket-client is a conversion of the Elm 0.18 WebSocket client to Elm 0.19, using ports instead of native code and an effects module.

Elm 0.19 shipped with no WebSocket client. It used to be in elm-lang/websocket. I have heard that its interface is being redesigned, and it will reappear sometime in the future. This package provides an alternative to use until then.

The package as shipped will work with a pure Elm WebSocket simulator, which transforms messages you send with a function you provide and sends the result back immediately. See the example README for instructions on setting up ports to make it use JavaScript code to do real WebSocket communication.

The example is live at billstclair.github.io/elm-websocket-client.

Keys and URLs

The old WebSocket package identified sockets by their URLs. You can do that with WebSocketClient if you want, by using the open function. But you can also assign a unique key to each connection, which enables multiple connections to a single URL, by using openWithKey. The key arg to the other action functions will be the URL if you used open or the key if you used openWithKey.

Using the Package

The Elm 0.18 WebSocket module, in the elm-lang/websocket package, was an effect module. This allowed it to update its state in the background, so your code didn't have to have anything to do with that. A regular port module isn't that lucky. The state for the WebSocketClient module needs to be stored in your application's Model, and you have to update it when you call its functions, or process a Value you receive from its subscription port.

See Main.elm and PortFunnels.elm in the the example/src directory for details. PortFunnels.elm exposes a State type and an initialState constant.

You will usually copy PortFunnels.elm into your application's source directory, and, if you use other PortFunnel modules, modify it to support all of them. It is a port module, and it defines the two ports that are used by example/site/index.html, cmdPort and subPort.

elm-websocket-client's People

Contributors

billstclair avatar

Watchers

James Cloos avatar Yuriy Habarov 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.