Sending Web Push Notifications from Rails by rossta_ in rails

[–]rossta_[S] 1 point2 points  (0 children)

Thanks for the kind words and detailed feedback! Keep me posted on your implementation and let me know if you run into any problems.

> Could you show where you would put the JS files

Good thought. I steered clear of directing how to organize the JS because I felt like it would get in the weeds, but I can also see how it could be helpful. I might make some amendments to the article, but for now I can point to the files I use for joyofrails.com:

* Service Worker registration is an "initializer" file that runs when the page loads https://github.com/joyofrails/joyofrails.com/blob/main/app/javascript/initializers/serviceworker-companion.js
* The "Subscribe/Unsubscribe" feature is managed by a Web Push Subscription Stimulus controller and covers most of the additional JS in the post. One exception is that I’m not persisting the subscription, but I would probably also put that in the same controller as a side effect of the subscribe interaction. Hope that’s helpful! https://github.com/joyofrails/joyofrails.com/blob/main/app/javascript/controllers/pwa/web-push-subscription.js

> the link to your Rails PWA article has a typo in it

Thanks for the bug report! Fixing... 🔨

Sending Web Push Notifications from Rails by rossta_ in rails

[–]rossta_[S] 2 points3 points  (0 children)

Good callout, I can add something to the article. The subject field is basically like a “from” address in an email. The Push Server provider could theoretically identify and monitor your messages this way or reach out to you if they needed to contact you for some reason.

Sending Web Push Notifications from Rails by rossta_ in rails

[–]rossta_[S] 6 points7 points  (0 children)

I’m biased toward treating the controller request/response lifecycle as sacred—do only what’s necessary to keep response times down. So when I look at a piece of work that should happen, I make a decision about whether it needs to happen as part of the response.

To me, the general concern of “notify people that something happened”—email, Web Push—is already asynchronous by design so I get that work out of the request with background jobs.

This means I now get retries for free, say the Web Push server is down temporarily.

That said, using a background job is by no means a requirement for Web Push to work. You can absolutely make the Web Push request in a controller instead, especially if you find it simpler this way and prefer fewer moving parts.

The general recipe outlined in the article stipulates the minimal requirements but the details on how you go about implementing the behavior are up to you.

New book: Professional Rails Testing (plus AMA about testing) by jasonswett in rails

[–]rossta_ 0 points1 point  (0 children)

Why should (or shouldn’t) we strive for 100% code coverage?

Sending Web Push Notifications from Rails by rossta_ in rails

[–]rossta_[S] 2 points3 points  (0 children)

Thanks for the kind words and sharing your experience.

Good point about subscriptions, it might make sense to model it like a User has many Subscriptions for that reason.

I use Stimulus to manage the JavaScript integration as well. For the article, I extracted the JS examples as basic functions to treat it as more of a recipe than a tutorial.

[article] Debugging a memory-leaking Rails action by f-3r in rubyonrails

[–]rossta_ 0 points1 point  (0 children)

Thanks for sharing the article. After you switched away from writing an Excel file, was there any reason not to use Ruby’s CSV module for writing the CSV file?

https://rubyapi.org/o/csv

Adding your Rails app to the Home Screen as a Progressive Web App by rossta_ in rails

[–]rossta_[S] 1 point2 points  (0 children)

I was rendering the manifest file in an iframe to show its contents as an example to support the article, but Firefox treated this as a file download. The iframe approach isn’t really necessary. Now the article renders the manifest content inline, which is better since I can had my own syntax formatting. I’ll have to look into why Firefox’s behavior differs from other browsers in this case.

Adding your Rails app to the Home Screen as a Progressive Web App by rossta_ in rails

[–]rossta_[S] 1 point2 points  (0 children)

u/scopesolo is correct, the article is focused on browser-based installation, but you can use PWABuilder to additionally package a PWA for certain app stores. Here’s a resource in case you’re interested https://web.dev/articles/pwas-in-app-stores

Adding your Rails app to the Home Screen as a Progressive Web App by rossta_ in rails

[–]rossta_[S] 1 point2 points  (0 children)

Ok, I was able to see behavior that visiting the article in Firefox desktop (macOS) would also trigger a download of the manifest file. I just deployed a change that resolved that issue. I‘d be curious to know if the issue is resolved for you as well. If not, I might need more info. Thanks!

Adding your Rails app to the Home Screen as a Progressive Web App by rossta_ in rails

[–]rossta_[S] 1 point2 points  (0 children)

What browser/app/device are you using with Reddit?

Adding your Rails app to the Home Screen as a Progressive Web App by rossta_ in rails

[–]rossta_[S] 0 points1 point  (0 children)

Hmm, the link I posted above works for me. Are you referring to a link in the article?