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

Live comment updates with ActionCable #2107

Merged
merged 53 commits into from
May 16, 2024
Merged

Live comment updates with ActionCable #2107

merged 53 commits into from
May 16, 2024

Conversation

nimmolo
Copy link
Contributor

@nimmolo nimmolo commented Apr 20, 2024

This PR lays the groundwork for live updates for any object via turbo_stream.

The first place I'm implementing them is Comments. Comments seem like a low-stakes place to test and get it right — it's not that they are so useful, but they're easy to test.

This PR is intended to clear a path for other uses of live updates on the app. The ones I'm more interested in are namings, user notifications, images (for image transformations that are run as a job) and observations, for the home page. These may require a bit of refactoring and renaming some templates.

Incredibly, it took only a few lines of code, but the configurations took quite some time to figure out.

Comments was the easiest to do because the comments panel has the same structure everywhere, and uses conventionally named partials and ivars.

Easiest way to try it out: use test.mushroomobserver.org in two different browser windows/accounts

Trying it out locally: prepare your local server

  1. Install redis server software locally.
    Linux: sudo apt-get install redis / Mac: brew install redis. You may also have to start the server: redis-server
  2. Install the gems on this branch, bundle install (installs puma locally)
  3. In one terminal window, run ./bin/dev or otherwise start Solid Queue - creates and updates are run as background jobs.
  4. In another terminal window, run rails server
  5. In a third terminal window, test your redis server connection. Run rails console --sandbox, then
redis = Redis.new
redis.ping

You should get a "PONG". If so, you can exit

  1. In two different browsers, say, Firefox and Chrome, or in a regular window and an "incognito" window, log in as two different users. Visit the same observation.
  2. Start posting comments on the observation in either browser.
  3. The comments should appear pretty quickly in the other browser without a page refresh, even if you have a comment form open.

Notes:

  • This will require installing redis on production

@nimmolo nimmolo changed the base branch from main to nimmo-field-slip-stimulus April 20, 2024 07:56
@coveralls
Copy link
Collaborator

coveralls commented Apr 20, 2024

Coverage Status

coverage: 94.434% (-0.02%) from 94.45%
when pulling 2c4ae53 on nimmo-actioncable
into 4f69b78 on main.

@nimmolo nimmolo changed the base branch from nimmo-field-slip-stimulus to main May 1, 2024 21:48
@nimmolo
Copy link
Contributor Author

nimmolo commented May 13, 2024

Ran into a problem testing based on the above instructions. After install redis, I think you have to start the server with redis-server in another shell. This could probably get added to the bin/dev stuff. Without it running I wan't able to get the "PONG" described above. Once it was running it seems to work.

Thanks. Added to the instrux

@nimmolo
Copy link
Contributor Author

nimmolo commented May 13, 2024

Now deployed on test.mushroomobserver.org and working.

Steps to deploy on production:

  • Install redis
  • Start redis: redis-server
  • Redis advises adding this line to /etc/sysctl.conf: vm.overcommit_memory=1
  • Edit /etc/nginx/nginx.conf on the test server, add a proxy_pass to allow traffic at /cable:
    # action cable websocket
    location = /cable {
      proxy_pass http://puma;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto https;
      proxy_redirect off;
    }

@nimmolo
Copy link
Contributor Author

nimmolo commented May 13, 2024

This is now working in a "production" environment at test.mushroomobserver.org. You can log in as yourself in a regular and "incognito"/"private" window and check it out.

Be careful if testing... any comments you post as tests will also post to the production database.

@nimmolo
Copy link
Contributor Author

nimmolo commented May 13, 2024

Remaining issues

  • Fixed. Comments are created with inappropriate controls, until the page is refreshed.
    • This is because when a comment is created via broadcast, the User.current is always the comment author. Action Cable does not have access to client sessions for the subscribers to the brodcast.
    • broadcast-compatible solution: print controls for all users and hide via CSS
  • Done. To Do: system test, two sessions

@nimmolo
Copy link
Contributor Author

nimmolo commented May 15, 2024

Recent changes:

  • Moved broadcast methods to the controller, from the model.
  • Added system tests with two sessions, to check broadcasts from each user, and via the API.
  • Broadcasts must be done from the model to catch CRUD from the API.

Disable rubocop class length
It needs to be there to register API crud!
Add tests for API comment crud actions
@nimmolo nimmolo changed the base branch from main to nimmo-system-test-class-selector-adjustments May 16, 2024 01:49
@nimmolo nimmolo changed the base branch from nimmo-system-test-class-selector-adjustments to main May 16, 2024 02:58
@nimmolo nimmolo merged commit f01bab2 into main May 16, 2024
2 of 5 checks passed
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.

None yet

3 participants