#205 Push Notifications with ActionCable

Summary

In this episode, we look into using Push Notifications within our Rails application and then using ActionCable to broadcast Push Notifications to the user.
rails websockets notifications actioncable 8:55

Resources

Can I Use - https://caniuse.com/#search=Push%20API
What Web Can Do Today - https://whatwebcando.today/

Summary

Terminalrails g channel notification
app/javascript/packs/application.jsNotification.requestPermission().then(function (result) {})
views/welcome/index.html.erb<%= link_to 'Send Notification', welcome_index_path, remote: true %>
views/welcome/index.js.erbif (Notification.permission === 'granted') {
  var title = 'Push Notification'
  var body = 'Triggered by link on the <%= Rails.env %> environment.'
  var options = { body: body }
  new Notification(title, options)
}
welcome_controller.rbdef index
  ActionCable.server.broadcast('notification_channel', 'You have visited the welcome page.')
end
app/channels/notification_channel.rbdef subscribed
  stream_from "notification_channel"
end
app/javascript/channels/notification_channel.jsimport consumer from "./consumer"

consumer.subscriptions.create("NotificationChannel", {
  connected() {},
  disconnected() {},
  received(data) {
    if (Notification.permission === 'granted') {
      var title = 'Push Notification'
      var body = data
      var options = { body: body }
      new Notification(title, options)
    }
  }
});