TL;DR
In the previous post, we loaded three Photos into the Picshare application. Today we will connect to the WebSocket server using Elm. This post is part of the functional language series, and it is based on a remarkable book, Programming Elm Build Safe and Maintainable Front-End Applications by Jeremy Fairbank.
WebSocket protocol enables communication through the same socket in both directions from the client to the server.
To connect to the WebSocket server, we need to use the javascript library. Elm has special modules where we can call javascript code. We call them ports
. In the port module, we define functions with keyword port
. Those functions will interact with javascript code. We create two-port functions, listen
and receive
.
Now we can write Javascript code in Picshare index.html
that will use those port
functions.
When you are done, import the WebSocket module in Picshare.elm file and compile:
elm make Picshare.elm --output picshare.js
We are redy to connect Picshare application with websocket server.
Comments are closed.