Code Monkey home page Code Monkey logo

action-cable-chat's Introduction

Build an ActionCable Chat Service

  1. New App

    # new rails app
    rails _5.0.0.beta3_ new ac && cd ac
    # initialize Git
    git init && git add .
    # first commit
    git commit -am "initial commit"
  2. Generate the Rooms Controller and Message Model

    rails g controller rooms show
    rails g model message content:text
    rails db:migrate
    # commit
    git add .
    git commit -am "Rooms Crontroller and Message Model"

    Add a test message:

    Message.create content: 'hello world'
  3. Update the routes to send root to rooms#show

    routes.rb:

    root to: 'rooms#show'
  4. Start the server and open the browser and show that the app is up

    rails server

    http://localhost:3000

  5. Set up the controller method and message views

    rooms_controller.rb:

    def show
      @messages = Message.all
    end
    

    show.html.erb

    _message.html.erb

    git add .
    git commit -am "Add rooms/message views"
  6. Pop open the browser again

    http://localhost:3000

  7. Generate our ActionCable room channel with 'speak' action:

    rails g channel room speak
  8. Uncomment App in cable.coffee and ActionCable mount in routes.rb

  9. Implement received and speak in room.coffee

  10. Implement speak in room_channel.rb

    def speak(data)
       ActionCable.server.broadcast 'room_channel', data['message']
     end
    git add .
    git commit -am "ActionCable Enabled"
  11. Restart Rails Server and Demo via JS console

  12. Add form field and required JavaScript to send messages..

  13. Remove

    from room.coffee received method

  14. Update channel 'speak' implementation to actually write to the db.

  15. Generate Job to handle Broadcasts

    rails g job BroadcastMessage
  16. Add after_create_commit hook to send broadcast job.

    after_create_commit { BroadcastMessageJob.perform_later self }
    git add .
    git commit -am "Fin"
  17. Restart rails server and demo working ActionCable in browser.

  18. Set up devise

    Add to the Gemfile:

    gem 'devise', github: 'plataformatec/devise'

    Install Devise and Generate User Model

    bundle install
    rails g devise:install
    rails g devise User
    rails g migration AddUserToMessages user:references:index
    rails db:migrate
    git add .
    git commit -am "Devise and User Model"
  19. Add belongs_to to Message Model:

    message.rb:

    belongs_to :user
  20. Update add devise before_action to controller:

    rooms_controller.rb:

    before_action :authenticate_user!
  21. Update v2 files

    application.html.erb _message.html.erb room.coffee rooms.coffee show.html.erb

  22. Explain current_user, warden config. Add warden hooks:

    Populate: config/initializers/warden_hooks.rb connection.rb

  23. Demonstrate in the browser. RESTART SERVER

  24. v3 STYLE

action-cable-chat's People

Contributors

jeffnappi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.