
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
elm-websocket-server
Advanced tools
Web Socket Server in Elm and Node.js
To install the elm part of this library run:
elm package install RGBboy/websocket-server
To install the node part of this library run:
npm install elm-websocket-server
See the example folder for a working version.
To get this working run:
npm install
npm run build:example
npm run start:example
Then go to localhost:8080 in multiple browser windows.
port module Server exposing (..)
import Platform exposing (Program)
import Json.Decode as Decode
import Json.Encode as Encode
import WebSocketServer as WSS exposing (Socket, sendToOne, sendToMany)
main : Program () Model Msg
main =
Platform.worker
{ init = always ([], Cmd.none)
, update = update
, subscriptions = subscriptions
}
-- PORTS
port inputPort : (Decode.Value -> msg) -> Sub msg
port outputPort : Encode.Value -> Cmd msg
-- MODEL
type alias Model = List WSS.Socket
-- UPDATE
type Msg
= Connection WSS.Socket
| Disconnection WSS.Socket
| Message String
| Noop
update : Msg -> Model -> (Model, Cmd msg)
update message model =
case (Debug.log "Msg" message) of
Connection socket ->
( socket :: model
, Cmd.none
)
Disconnection socket ->
( List.filter ((/=) socket) model
, Cmd.none
)
Message clientMessage ->
( model
, WSS.sendToMany outputPort clientMessage model
|> Cmd.batch
)
Noop -> (model, Cmd.none)
-- SUBSCRIPTIONS
decodeMsg : Decode.Value -> Msg
decodeMsg value =
let
decoder = WSS.eventDecoder
{ onConnection = (\socket _ -> Connection socket)
, onDisconnection = (\socket _ -> Disconnection socket)
, onMessage = (\_ _ message -> Message message)
}
in
Decode.decodeValue decoder value
|> Result.withDefault Noop
subscriptions : Model -> Sub Msg
subscriptions model = inputPort decodeMsg
The node API for this is quite simple:
var port = (process.env.PORT || 8080),
server = require('http').createServer(),
WebSocketServer = require('elm-websocket-server'),
app = require('./my-elm-server.js').Elm.Server.init(),
wss = new WebSocketServer(
server,
app.ports.inputPort,
app.ports.outputPort
);
server.listen(port);
console.log(`Listening on :${port}`);
Run npm install
then elm package install
.
Run npm test
.
FAQs
Web Socket Server in Elm and Node.js
The npm package elm-websocket-server receives a total of 0 weekly downloads. As such, elm-websocket-server popularity was classified as not popular.
We found that elm-websocket-server demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.