Git Product home page Git Product logo

action-cable-example's Introduction

Action Cable Example

Let's build a chat with ActionCable!

This kata borrows heavily from Sophie DeBenedetto's awesome tutorial on ActionCable.

This app is a small chat system: users can send messages on different channels. However, all the pages are static, which means you have to refresh the page to see if new messages have been posted.

The point of this kata is to fix that by using ActionCable.

Setup

You'll need to be running ruby 2.3.0 (we recommend using rbenv to manage multiple versions of ruby).

ActionCable uses Redis to persist connection data. You can install Redis like this:

  • For Mac OS: brew install redis
  • For Linux:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make install

We'll also be using PostgreSQL as our database. On Mac OS, you can install it with brew install postgres. You'll find instructions here for Linux.

And of course, once you've forked this repo, you need to run bundle install.

Create the database with rake db:create

Migrate the database with rake db:migrate

Domain

The only objects we're dealing with are users, channels and messages. Check out the models and the db/schema.rb file to understand them.

Using ActionCable

First, we need to modify the html so that sending the message happens by pressing Enter instead of submitting the form. Add this to app/assets/javascripts/chatrooms.js:

$(document).ready(function() {
  submitNewMessage();
});

function submitNewMessage(){
  $('textarea#message_content').keydown(function(event) {
    if (event.keyCode == 13) {
        $('[data-send="message"]').click();
        $('[data-textarea="message"]').val(" ")
        return false;
     }
  });
}

Then, follow the steps from the original article.

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.