Skip to content

Latest commit

 

History

History
53 lines (35 loc) · 2.65 KB

README.md

File metadata and controls

53 lines (35 loc) · 2.65 KB

Hotwire Go Example

This is a recreation of the Hotwire Rails Demo Chat with a Go backend. See the Hotwire docs for more information about Hotwire.

Quickstart

Requirements

  • Go
  • NPM

Set up the project

Automatic

Run ./scripts/setup.sh.

Manual

  1. Download Go dependencies: go mod download
  2. Download node dependencies: npm install
  3. Create a webpack bundle: npx webpack -c webpack.config.js

Run the project

The demo can be run with go run main.go from the project root.

Packages used

Ruby On Rails provides a wealth of functionality out of the box. While batteries-included frameworks for Go web development exist (e.g. Buffalo), it's often more idiomatic to compose functionality from small, single-purpose libraries. In order to mimic some of Rails' functionality, the following packages were used:

  • Chi provides request routing and middleware support.
  • Render provides template rendering.
  • GORM provides database ORM functionality.
  • Websocket provides websocket connectivity.

In addition, the pkg directory contains a few purpose-built packages that mimic Rails functionality for the demo:

  • pubsub implements in-memory PubSub functionality that fills the role of Rails' ActionCable.
  • notify implements a cookie-based notification banner.
  • timefmt implements a helper function for formatting Go's time.Time type into a similar format to the Rails example.

Notable differences from the Rails demo

  • The Rails demo uses a Stimulus controller to do form resetting when submitting a new chat message. While Stimulus is a great framework, in Go it's generally idiomatic to reimplement small pieces of functionality instead of adding a new dependency, and because the reset controller functionality is easily implemented in pure Javascript, it felt more idiomatic to leave Stimulus out in this case.
  • Turbo Rails, and the Rails example by extension, relies heavily on ActionCable to connect Turbo Streams to a websocket. Because there is no ActionCable equivalent in Go, this example uses an in-memory pubsub mechanism and a TurboStreamWebsocketSource custom element to mimic the same functionality. In a production setting, you would probably want to use a more robust PubSub mechanism.