This is a recreation of the Hotwire Rails Demo Chat with a Go backend. See the Hotwire docs for more information about Hotwire.
- Go
- NPM
Run ./scripts/setup.sh
.
- Download Go dependencies:
go mod download
- Download node dependencies:
npm install
- Create a webpack bundle:
npx webpack -c webpack.config.js
The demo can be run with go run main.go
from the project root.
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'stime.Time
type into a similar format to the Rails example.
- 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 aTurboStreamWebsocketSource
custom element to mimic the same functionality. In a production setting, you would probably want to use a more robust PubSub mechanism.