Santiago Andrés Rodríguez Márquez
Complete-stack Developer, chess newbie, languages geek.
There are occasions when one as a Ruby on Rails developer desires to enforce a real-time characteristic, like a talk application, and after digging a little you discover a framework’s characteristic named ActionCable, sounds new and frightening, proper? Worry no longer, I will take a look at to give an explanation for it so simple as imaginable in order that on the finish of this newsletter you’ll be able to really feel at ease with the topic.
As a result of we’re truly busy and our best passion is the right way to enforce the real-time options to our Ruby on Rails application I will proceed with the stairs required to arrange ActionCable. The next steps think that you’re acquainted with Ruby on Rails and you might be the use of a model of it more than 5.2.
1. To your Gemfile, upload the redis gem and run package deal set up.
mount ActionCable.server, at: '/cable'
, you guessed it, ‘/cable’ is the default trail.
<%= action_cable_meta_tag %>
on the head of your primary structure template.
rails g channel messages
on the terminal.
# app/channels/messages_channel.rb elegance MessagesChannel < ApplicationCable::Channel def subscribed stream_from 'messages' finish finish
manner is a hash that will probably be serialized as JSON and gained together with the shopper.
# app/controllers/messages_controller.rb elegance MessagesController < ApplicationController def create # ... if message.save ActionCable.server.broadcast( 'messages', frame: message.frame, writer: message.writer ) #... finish # ... finish finish
# config/cable.yml manufacturing: adapter: redis url: redis://localhost:6478/1 # ...
Subscribe to get your day-to-day round-up of most sensible tech tales!