Using Electron with Haskell

Haskell-Electron-app/
haskell-app/
resources/
...
...electron files
backend/
stack.yaml
backend.cabal
...servant files

Setting up Electron

$ cd Haskell-Electron-app
$ git clone https://github.com/electron/electron-quick-start haskell-app
$ cd haskell-app
$ npm install && npm start

Setting up the Haskell webserver

$ cd Haskell-Electron-app
$ stack new backend servant
$ cd backend
$ stack build

Contacting Servant/Haskell from Electron

Launching the Haskell web server from Electron

$ cd Haskell-Electron-app/backend
$ stack build --copy-bins
  • We are using the child_process.spawn command to launch our backend web server
  • We imported the child_process module with const child_process = require('child_process')
  • Defined a variable let backendServer that'll let us keep the backend server from being garbage collected
  • Added a function createBackendServer that runs child_process.spawn('./resources/backend-exe') to spawn the process
  • Added the createBackendServer function to the ready hook with app.on('ready', createBackendServer)
  • Close the backendServer when the event will-quit occurs

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christian Kjær

Christian Kjær

Helping Famly grow 🌱🚿 • Previously built the IoT platform at Blackbird • Rust/Haskell/Serverless enthusiast ☁️