Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Rails app #125

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

Envek
Copy link
Contributor

@Envek Envek commented Jun 20, 2023

I made a dead simple Rails application that demonstrates how to use NATS Ruby client withing Rails application using new additions.

However, I'm incredibly bad at coming up with arbitrary examples, so for now idea is dumb: there is a web form which upon submit doesn't create a database record directly (as most typical applications do) but instead publishes a message into NATS and let different process to receive it and create database record instead.

Share your ideas, so I can add more interesting stuff.

Also for now it is not very pleasantly looking one (but this also can be fixed):
image

README.md

Files of interest:

  • config/initializers/nats.rb — initialization of the NATS client early using lazy connection.
  • app/controllers/test_controller.rb — example of the controller.
  • bin/nats-listener — example of the separate standalone process.

Main client features shown in the example:

  • Thread-safety and fork handling: it is totally fine to use “global” NATS client instance without any connection pooling.
  • Rails development mode code reloading and resource handling: no more server restarts on changes and leaked database connections.

A (somewhat artificial) distributed workflow:

  • User fills the form and submits it.
  • The form is broadcasted via NATS using “global” NATS client instantiated in the web application server “master” process, but usable in worker processes.
  • Special long running process (listener) subscribes to the broadcasted messages and saves them to the database.
  • User will eventually see saved messages on the page with the form.

Installation

This app has a Docker-first configuration based one the Ruby on Whales post.

You need:

Run the following command to build images and provision the application:

dip provision

Running

You can start Rails server along with AnyCable by running:

dip up web listener

Then go to http://localhost:3000/ and see the application in action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant