Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

Embed Size (px)

Citation preview

  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    1/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 1

    Joseph Ndungu (/)

    PROJECTS

    (/PROJECTS)

    TUTORIALS

    (/TUTORIALS)

    CONTACT

    (/CONTACT) (HTTPS://WWW.FACEBOOK.COM/PROFILE.

    ID=100000188533185)(HTTPS://TWITTER.COM/_JOWSE)

    GMAIL LIKE CHAT APPLICATION IN RUBY ON RAILS

    30 Jul 14

    jquery (/tags/jquery), rails (/tags/rails), chat (/tags/chat)

    In this tutorial, I will guide you on how to implement a Gmail/Facebook like real-time chat application in a Ruby on Rails

    application.

    ___

    Guide To Hacking AdWordsHow to Boost Your PPC Performance. Download Your Free Guide Now!

    https://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttps://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttps://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttps://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttps://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttp://josephndungu.com/http://josephndungu.com/https://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttps://www.googleadservices.com/pagead/aclk?sa=L&ai=C0_dq79E5V-PBGquDzAaVsp-QDoPg0NZElJrq-NkC5KWn2vICEAEgr-HuG2DH1f-NhCagAZKxw-8DyAEBqAMByAPDBKoErQFP0GdCroeF6tNh4Jj7WT-U12sGBulPwsFNAF_KO1zEW29q_xhBunTwqJxCv3GTdsrv4rPUaPFTFMkVZ5ArfM9KjEMF573gtI__OVinvKCKAccE5e7gPFoN0g_xte_Ko1aYztgD_S00wooo3TWJVKuAZdDWuDTWZ0c-Yqop-_F3sbKZ94cltkTb9l5yiDxbnwgyOdGsFnmQLRZYWes454PbC1KNtxntxvYoVM3jkogGAYAH1s68EKgHpr4b2AcB&num=1&cid=CAASEuRoB8pe0hhOvS2jA_suBJ1e4w&sig=AOD64_1I7nIXGpxlOZxaP9DiXSLPmjLYyg&client=ca-pub-0765173663573649&adurl=http://marketing.wordstream.com/WSContentDL030615HackingAdWords.htmlhttp://josephndungu.com/tags/chathttp://josephndungu.com/tags/railshttp://josephndungu.com/tags/jqueryhttps://twitter.com/_Jowsehttps://www.facebook.com/profile.php?id=100000188533185http://josephndungu.com/contacthttp://josephndungu.com/tutorialshttp://josephndungu.com/projectshttp://josephndungu.com/
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    2/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 2

    INTRODUCTION

    We are all fond of the Gmail and Facebook inline chat modules. About a week ago, I came across a tutorial on how to replicate

    hangouts chat (http://css-tricks.com/replicating-google-hangouts-chat/) in pure css and html and this really inspired me. I though

    myself, "why not give life to this beautiful Gmail-like chat UI?". With this, I challenged myself to try and come up with a real-time ch

    in a Rails application. I wanted to have anear-realtime kind of chat functionality and in my research I found out that the

    publish/subscribe model was the most efcient, fast and scalable way to go about this.

    As a framework, Rails is not very good at handling asynchronous events and therefore establishing a socket connection to a Rails

    application is almost impossible. There are many solutions that are designed to handle this kind of problem. Frameworks such as

    Node.js (http://nodejs.org/) with Socket.IO (http://socket.io/), or, if you want to stick with Ruby, Cramp

    (https://github.com/lifo/cramp), async_sinatra (https://github.com/raggi/async_sinatra), or Goliath framework (http://postrank-

    labs.github.com/goliath/) are all great solutions but it would be super awesome to continue using Rails for our application's logic a

    also have the benets of some kind of asynchronous event handling with publishing and subscribing when we need that. To achiev

    this, we will use Private Pub Gem (https://github.com/ryanb/private_pub) which is built on top of Faye (http://faye.jcoglan.com/) a

    makes it dead simple to publish and subscribe to real-time events in a Rails app.

    Check out The Demo (http://railschattyapp.herokuapp.com/) to have a feel of what we will be creating. Create an account on one

    separate browser window and on another log in using the default user provided on the login page then try simulating a conversat

    between yourself and the "chatty bot".

    GETTING STARTED

    Were going to add the Instant Messaging feature to an existing Rails application. Below is a screenshot from a simple rails app

    (chatty) that uses Devise (https://github.com/plataformatec/devise) to authenticate users. It's current functionality is very basic. O

    the home page, we are just looping the list of all users except the currently logged in user.

    We want to enable the users of our app to chat with each other. Let's start by modeling up the logic of our chat application. Our lois very simple. A user will have many conversations and a conversation will have many messages. Here is the relationship diagram

    https://github.com/plataformatec/devisehttp://railschattyapp.herokuapp.com/http://faye.jcoglan.com/https://github.com/ryanb/private_pubhttp://postrank-labs.github.com/goliath/https://github.com/raggi/async_sinatrahttps://github.com/lifo/cramphttp://socket.io/http://nodejs.org/http://css-tricks.com/replicating-google-hangouts-chat/
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    3/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 3

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/create_conversations.rb)

    CONVERSATION MODEL

    Let's get started by creating the conversation model. A conversation will hold the sender_id and the recipient_id both of which are

    instances of a user. The sender_id will hold the id of the user starting the conversation and the recipient_id will hold the id of the

    other user.On your terminal

    $ rails g model Conversation sender_id:integer recipient_id:integer

    It's a good idea to add an index to both sender_id and recipient_id as we will use this elds while searching.

    1

    2

    3

    4

    5

    67

    8

    9

    10

    11

    12

    13

    create_conversations.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-create_conversations-rb)hosted with by

    GitHub (https://github.com)

    After migrating the database, lets update our user model. Since we don't have theuser_id column in our conversation table, we mu

    explicitly tell rails which foreign key to use.

    1

    2

    3

    4

    class CreateConversations < ActiveRecord::Migration

    def change

    create_table :conversations do |t|

    t.integer :sender_id

    t.integer :recipient_id

    t.timestamps

    end

    add_index :conversations, :sender_id

    add_index :conversations, :recipient_id

    end

    end

    class User < ActiveRecord::Base

    devise :database_authenticatable, :registerable,

    :recoverable, :rememberable, :trackable, :validatable

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-create_conversations-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/create_conversations.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    4/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 4

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/user.rb

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/conversation.rb

    5

    6

    7

    user.rb

    (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-user-rb)hosted with by GitHub (https://github.com)

    Next, lets edit our conversationmodel. A conversation will belong to both a sender and a recipient all of which are instances of auser.

    1

    2

    3

    4

    5

    6

    7

    8

    910

    11

    12

    13

    14

    15

    16

    conversation.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-conversation-rb)hosted with by GitHub

    (https://github.com)

    As you can see, our conversation will have many messages. We are then validating uniqueness of the sender_id and passing a scop

    of the recipeint_id. What this does is that it ensures that thesender_idand the recipient_id are always unique so that we only have

    unique conversations in our application. For instance, a conversation with (sender_id: 1, recipient_id: 2) and another with

    (sender_id: 2, and recipient_id: 1) should never occur since the conversation is essentially between the same users.

    We have also added two scopes involving and between. The rst scope will help us retrieve all conversations of the currently

    logged-in user while the last scope will help us check if a conversation exists between any given two users before we create the

    conversation.

    MESSAGE MODEL

    Now that we have our conversation model, lets proceed and create our message model. Run the following command on your

    terminal and run rake db:migrate.

    $ rails g model Message body:text conversation:references user:references

    Our messages table will have a body, conversation_id, and user_id columns. The conversation_id column will keep track of which

    conversation a message belongs to and the user_id column will keep track of the user who sent the message during chat. Adding

    some validations

    has_many :conversations, :foreign_key => :sender_id

    end

    class Conversation < ActiveRecord::Base

    belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'

    belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'

    has_many :messages, dependent: :destroy

    validates_uniqueness_of :sender_id, :scope => :recipient_id

    scope :involving, > (user) do

    where("conversations.sender_id =? OR conversations.recipient_id =?",user.id,user.id)

    end

    scope :between, > (sender_id,recipient_id) do

    where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND

    end

    end

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-conversation-rbhttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-user-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/conversation.rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/user.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    5/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 5

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/message.rb

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/user.html.erb

    1

    2

    3

    4

    5

    6

    message.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-message-rb)hosted with by GitHub (https://github.com)

    LOGIC FLOW

    Now that our models are in place, I want to explain the general ow of how the inline chat application will work. On our home pag

    we will add a send message button along each user's name. The button will holdtwo data attributes i.e. the id of the current user

    and another id of the reciever id. When a user clicks the button, we will send an asynchronous request to our rails app with the

    current user's id and the recipient id. If a conversation exists, we return the conversation id immediately, otherwise we create the

    conversation and return the id of the newly created conversation.

    We will then pick, the conversation_id returned by the server using jQuery. Using this conversation_id, we will then request the

    respective show page of that conversation which will have a list of all its associated messages. For instance, if the server returnsconversation_id3, we will request the html page at conversations/3. We will then append this conversation data in our home page

    a popup div.

    ADDING THE MESSAGE BUTTON

    For each user we are displaying on our home page, we associate a send message button with him or her. Thedata-sid attribute wil

    hold our current users' id whiledata-rip attribute will hold the recipient's id. We will send this values through an ajax request to o

    server which should create a conversation if necessary.

    index.html.erb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    user.html.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-user-html-erb)hosted with by GitHub (https://github.com)

    To keep this tutorial, short I have created most of the jQuery logic in a single le which I will link here. I have tried to make it as

    modular as possible and also provide comments on each method so it shouldn't be hard to comprehend what's cooking. Lets creat

    le chat.js inside our javascripts folder and paste the contents of chat.js (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175#le-chat-js). This le has all the functions we will need when creating chatboxes in our home page an

    also communicating with our rails app.

    Let's not forget to add this le to our javascript manifest le.

    class Message < ActiveRecord::Base

    belongs_to :conversation

    belongs_to :user

    validates_presence_of :body, :conversation_id, :user_id

    end

    current_user.id, "datarip" => user.id %>

    https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-chat-jshttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-user-html-erbhttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-message-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/user.html.erbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/message.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    6/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 6

    If using rails 4 and turbolinks, require this le before requiring turbolinks.

    //= require chat

    In my users.js we'll listen for various events on our home page document and call respective methods inside ourchat.js le

    users.js

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    var ready = function () {

    /**

    * When the send message link on our home page is clicked

    * send an ajax request to our rails app with the sender_id and

    * recipient_id

    */

    $('.startconversation').click(function (e) {

    e.preventDefault();

    var sender_id = $(this).data('sid');

    var recipient_id = $(this).data('rip');

    $.post("/conversations", { sender_id: sender_id, recipient_id: recipient_id }, function (data) {

    chatBox.chatWith(data.conversation_id);

    });

    });

    /**

    * Used to minimize the chatbox

    */

    $(document).on('click', '.toggleChatBox', function (e) {

    e.preventDefault();

    var id = $(this).data('cid');

    chatBox.toggleChatBoxGrowth(id);

    });

    /**

    * Used to close the chatbox

    */

    $(document).on('click', '.closeChat', function (e) {

    e.preventDefault();

    var id = $(this).data('cid');

    chatBox.close(id);

    });

  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    7/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 7

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/users.js

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    users.js

    (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-users-js)hosted with by GitHub (https://github.com)

    CREATING CONTROLLERS

    We now have our javascript les logic ready all is left is piecing up ourconversations and messages controllers to handle requests

    from our javascript le. Let's start by our conversations controller

    $ rails g controller conversations

    Our conversations controller will have only to actions, thecreate and the show actions.

    conversations_controller.rb

    1

    2

    3

    4

    5

    /**

    * Listen on keypress' in our chat textarea and call the

    * chatInputKey in chat.js for inspection

    */

    $(document).on('keydown', '.chatboxtextarea', function (event) {

    var id = $(this).data('cid');

    chatBox.checkInputKey(event, $(this), id);

    });

    /**

    * When a conversation link is clicked show up the respective

    * conversation chatbox

    */

    $('a.conversation').click(function (e) {

    e.preventDefault();

    var conversation_id = $(this).data('cid');

    chatBox.chatWith(conversation_id);

    });

    }

    $(document).ready(ready);

    $(document).on("page:load", ready);

    class ConversationsController < ApplicationController

    before_filter :authenticate_user!

    layout false

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-users-jshttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/users.js
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    8/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 8

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/conversations_controller.rb)

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    conversations_controller.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-conversations_controller-rb)hosted with

    byGitHub (https://github.com)

    You will notice, the layout false directive. This is because we don't want the show view inheriting from application.html layout. We

    will be appending this view on our home page.

    In the create action, we start off by searching if a conversation exists between thesender_id and recipient_id. Recall our between

    scope we dened earlier in our conversation model. If a conversation is found, we return it and assign it to the@conversation

    instance variable. If no conversation was found between the two users, we create a new conversation. We then eventually return

    json response with theid of the conversation.

    Our show action is also pretty standard, after getting the conversation with the specic id, (note this id is sent by our javascript co

    in chat.js), we also nd who is receiving the message. We will use the receiver's name as the chatbox title in our chat.

    INSTALLING PRIVATE_PUB

    It would be nice if we installed private_pub (https://github.com/ryanb/private_pub) at this point as our next set of task, we will be

    utilizing functionality offered by the gem. Add private_pub gem in your Gemle and runbundle. Installing it will also install Faye an

    its dependencies. We will also include the thingem as we will be using it to serve Faye.

    gem 'private_pub'

    gem 'thin'

    def create

    if Conversation.between(params[:sender_id],params[:recipient_id]).present?

    @conversation = Conversation.between(params[:sender_id],params[:recipient_id]).first

    else

    @conversation = Conversation.create!(conversation_params)

    end

    render json: { conversation_id: @conversation.id }

    end

    def show

    @conversation = Conversation.find(params[:id])

    @reciever = interlocutor(@conversation)

    @messages = @conversation.messages

    @message = Message.new

    end

    private

    def conversation_params

    params.permit(:sender_id, :recipient_id)

    end

    def interlocutor(conversation)

    current_user == conversation.recipient ? conversation.sender : conversation.recipient

    end

    end

    https://github.com/ryanb/private_pubhttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-conversations_controller-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/conversations_controller.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    9/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 9

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/show.html.erb

    Next up, we need to run the generator provided by private_pub to generate the conguration le and a Rackup le for starting the

    Faye server.

    rails g private_pub:install

    We can now start up that Rack server by running this command on a separate terminal

    rackup private_pub.ru -s thin -E production

    This command starts up Faye using the Thin server in the production environment which is necessary to get Faye working. The las

    step is to add private_pub to the applications JavaScript manifest le.

    //= require private_pub

    With private_pub installed, we can now proceed to make our rst view that uses private_pubs functionality. This will be the show

    view of our conversation's controller

    show.html.erb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    show.html.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-show-html-erb)hosted with by GitHub

    (https://github.com)

    @conversa

    @conversation


    true, :html => {id: "conversation_form_#{@conversation.id}

    @conversation.id %>

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-show-html-erbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/show.html.erb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    10/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 10

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_controller.rb)

    What stand's out here is the suscribe_to method. Here we subscribe to a channel ( the conversation's path ). We will use this same

    path to publish update notications to this channel from our controller. We do this by callingsubscribe_toand passing in the name

    of a channel which takes a format of a path. In our case we pass the current conversation's path.

    The suscribe_to function is provided by the private_pub javascript we required inside our application.js manifest le. We also noti

    that our form will always be submitting via ajax due to theremote option.

    Our last controller is the messages controller.

    $ rails g controller messages

    messages_controller.rb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    messages_controller.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-messages_controller-rb)hosted with by GitHub

    (https://github.com)

    We only have the create action for the messages controller. We store theconversation's path in the @path instance variable. We

    will use this path topublishpush notications to our view. Remember we suscribed to the same url in our conversation's show vie

    This will always be unique for any two users in our app.

    Our create action will render a javascript template. Let's go ahead and create that. In our messages folder create this le

    create.js.erb

    1

    2

    3

    4

    5

    6

    7

    class MessagesController < ApplicationController

    before_filter :authenticate_user!

    def create

    @conversation = Conversation.find(params[:conversation_id])

    @message = @conversation.messages.build(message_params)

    @message.user_id = current_user.id @message.save!

    @path = conversation_path(@conversation)

    end

    private

    def message_params

    params.require(:message).permit(:body)

    end

    end

    var id = "";

    var chatbox = $("#chatbox_" + id + " .chatboxcontent");

    var sender_id = "";

    var reciever_id = $('meta[name=userid]').attr("content");

    chatbox.append("");

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-messages_controller-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_controller.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    11/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 11

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/create.js.erb

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/meta.html.erb

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/_message.html.erb)

    8

    9

    10

    11

    12

    13

    14

    15

    16

    create.js.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-create-js-erb)hosted with by GitHub (https://github.com)

    Here we publish to the same path we suscribed to in our view. We also asign a number to variables from our rails app to variables

    our javascript le. You are probably wondering what the following line does?

    var reciever_id = $('meta[name=user-id]').attr("content");

    We need a way to identity who is the recipient when publishing the notications. A simple way around this is to create a meta tag our application.html layout le that store's the id of the currently logged in user. In the head section of our layout le add

    1

    meta.html.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-meta-html-erb)hosted with by GitHub (https://github.com

    You will also note that we are rendering amessage partial. We have not created that yet. In our messages folder

    _message.html.erb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    _message.html.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-_message-html-erb)hosted with by GitHub

    (https://github.com)

    Note, we have used to helper methods self_or_other and message_interlocutor. They both take a message as an argument. Lets go

    ahead and dene them. In our messages_helper le

    messages_helper.rb

    chatbox.scrollTop(chatbox[0].scrollHeight);

    if(sender_id != reciever_id){

    chatBox.chatWith(id);

    chatbox.children().last().removeClass("self").addClass("other");

    chatbox.scrollTop(chatbox[0].scrollHeight);

    chatBox.notify();

    }

  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    12/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 12

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_helper.rb)

    view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/routes.rb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    messages_helper.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-messages_helper-rb)hosted with by GitHub

    (https://github.com)

    Since we created our conversation's controller and messages controller, we have not yet dened their respective routes. Let's add

    them right away

    routes.rb

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1011

    12

    13

    14

    15

    16

    17

    18

    routes.rb

    (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-routes-rb)hosted with by GitHub (https://github.com)

    Notice we don't have yet any stylings for our chat box. Create a lechat.css in our stylesheets folder and paste the contents of

    chat.css (https://gist.github.com/Joseph-N/d1413cc5822efa416175#le-chat-css). Let's also add the fon't awesome css in our

    application.html layout. This will provide us with some nice icon stylings for the minimize and close buttons.

    application.html.erb

    1

    2

    module MessagesHelper

    def self_or_other(message)

    message.user == current_user ? "self" : "other"

    end

    def message_interlocutor(message)

    message.user == message.conversation.sender ? message.conversation.sender : message.conversation.recipient

    end

    end

    Rails.application.routes.draw do

    devise_for :users

    authenticated :user do

    root 'users#index'

    end

    unauthenticated :user do

    devise_scope :user do get "/" => "devise/sessions#new"

    end

    end

    resources :conversations do

    resources :messages

    end

    end

    https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-chat-csshttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-routes-rbhttps://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-messages_helper-rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/routes.rbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_helper.rb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    13/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 13

    view raw (https://gist.github.com/Joseph-

    N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/application.html.erb)

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    application.html.erb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-application-html-erb)hosted with by GitHub

    (https://github.com)

    TESTING OUR CHAT APP

    Here comes the most interesting part of the tutorial. Our application should now be capable of handlingnear-realtimeinstant

    messages between any two users. In one browser window, I'm logged in as "Joseph" and in another i'm logged in as "Kevin". As

    "Joseph", clicking on Kevin's send message button, creates a new conversation between us and pops up a beautiful chat window. O

    my other browser as "Kevin" clicking on Joseph's send message button pops up a similar window and we can now chat instantly!.

    Check it out

    Chatty

    true %>

    true %>

    https://github.com/https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-application-html-erbhttps://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/application.html.erb
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    14/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 14

    And that's it for this tutorial. Hope this gives you an overview on how to go about implementing an instant message type chat for

    your rails application. Have any questions and or comments? Feel free to use the comments section below. Something not workin

    out? feel free to also clone this application on my Github repo (https://github.com/Joseph-N/chatty) and compare.

    Happy Hacking!

    UPDATE 12/04/2015

    For those of you looking to setup your own instance of FAYE server on Heroku, take a look at an example FAYE app

    (https://github.com/Hareramrai/fayeserver)

    BY JOSEPH NDUNGUI'm a passionate web developer specializing in ruby on rails. This is my notepad where I share code snippets andtutorials of the things I have learnt and amassed over the years in my web development career. When not geeking

    out over web development, I'm likely out at the swimming pool or with friends catching a movie. Find me on

    Twitter (https://twitter.com/_Jowse) and Google Plus (https://plus.google.com/u/0/100660615792980129786)

    https://www.googleadservices.com/pagead/aclk?sa=L&ai=Co0QV8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGAWIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_10bvaW-nQ7cdDnk4IDzGOdNq00og&adurl=http://www.spree.co.za/tokyo-laundry-faux-fur-trim-parka-jacket-khaki-green/product/CY5X8XU&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=CKcSW8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGASIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_3OCcN-jjpQlBxnkBD-qnHXeUrdIg&adurl=http://www.spree.co.za/superga-high-top-sneakers-with-zip-detail-white/product/9GV25S5&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=Chhcq8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGAOIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_29RCuWQgNeG4U-osoAVtDn0Aup2A&adurl=http://www.spree.co.za/amanda-laird-cherry-mia-dress-pale-pink/product/2AFDZOA&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=CPqs18tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGAKIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_2JLTSptYi9Esbhv2AdP7hVrJGdTQ&adurl=http://www.spree.co.za/miss-black-slip-on-laceless-broque-black/product/KK4E288&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=C2Vb-8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGAGIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_1GlQA-TOXBl3yr02dxttila2B1mQ&adurl=http://www.spree.co.za/miss-black-lace-up-broques-tan/product/6426Y2X&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=Ca0zc8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGACIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_1_HPWQndQnlF4QFWBUqoFqjbjwMQ&adurl=http://www.spree.co.za/jordan-crew-sneakers-black/product/3MF9RMF&client=ca-pub-0765173663573649https://www.googleadservices.com/pagead/aclk?sa=L&ai=Ca0zc8tE5V4DTJ-yEzAb7976ADtqVmM5E9fvIsJUCtd2hhZoEEAEgr-HuG2DH1f-NhCagAbvh-NUDyAEJqQLaJvqen0RxPqgDAaoEtAFP0PRYS6sYmp3nGNA21BDuz4UJVWyokYBNhA4qRsqBmcevRS5J4X_SHXZ3R2hEb-vl0_dF8T_HichIJRoQyrRCoDmjQBgOgGcpJmZVAT8w7BKzGnigTc0OGGF_HbkeeGr45zReTAUWNmtmhQHHbvlDFey7pl4QmCQKUjdXtmSbqiLf5GbwdxKQg2fvXsB8ACxMiXk6_kpolMxEZz85wk80Q5XuAT_Dwpn9QDXcEy3S71-Adgr6BQYIJRABGACIBgGgBi6AB62ehyqoB6a-G9gHANgTCA&num=1&cid=CAASEuRo2wp162EVZAIGetBlMUdcVA&sig=AOD64_1_HPWQndQnlF4QFWBUqoFqjbjwMQ&adurl=http://www.spree.co.za/jordan-crew-sneakers-black/product/3MF9RMF&client=ca-pub-0765173663573649https://plus.google.com/u/0/100660615792980129786https://twitter.com/_Jowsehttps://github.com/Hareramrai/fayeserverhttps://github.com/Joseph-N/chatty
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    15/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 15

    < PREV (/TUTORIALS/FAST-AUTOCOMPLETE-SEARCH-TERMS-RAILS)

    NEXT > (/TUTORIALS/MANAGING-ENV-VARIABLES-IN-RAILS)

    (https://github.com/thomasquiroga)

    (https://github.com/santhu46)

    (https://github.com/kumrzz)

    (https://github.com/lawrenceN)

    85 COMMENTSTHOMAS QUIROGA06 Aug 14

    Huge thanks, it's really a good work!

    Reply |View 0 Replies

    SANTHU4611 Aug 14

    Hey wonderful tutorial works ne. I need to develop Same functionality for mobile (IOS and

    andriod chat using back end as a rails ) rather than web, Please help if u have any idea. Helpful..

    Reply |View 1 Reply

    KUMRZZ20 Aug 14

    love it, nice to see a relevant and recent app on privatepub: thanks! I was tearing my hair out failin

    to get such an app out ... one suggestion though, I think its best to have more than one user setup i

    seeds.rb coz otherwise when u start out u can't start chatting(and its not straightforward creating

    one using the base CRUD on the app) Also think it'll be nice to add basic setup instrns such as

    bundle install and so on. ... happy to enter a pull etc.

    Reply |View 0 Replies

    LAWRENCEN04 Sep 14

    This is a nice idea man and very clear writing.

    Reply |View 0 Replies

    ASHISHGOOJAR23 Sep 14

    ___

    https://github.com/ashishgoojarhttps://github.com/lawrenceNhttps://github.com/kumrzzhttps://github.com/santhu46https://github.com/thomasquirogahttp://josephndungu.com/tutorials/managing-env-variables-in-railshttp://josephndungu.com/tutorials/fast-autocomplete-search-terms-rails
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    16/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 16

    (https://github.com/ashishgoojar)

    (https://github.com/abdugaliyev)

    (https://github.com/tedma4)

    (https://github.com/vtorres)

    (https://github.com/liangtfm)

    (https://github.com/mcoudsi)

    Hi JOSEPH. Great job done. really ice tutorial. I tried to use the codes in my app and it was

    working in local machine. But i am unable to make it work once deployed on Heroku. Nothing

    happens once I click on "Send Message". Any help??

    Reply |View 1 Reply

    ABDUGALIYEV07 Oct 14

    Hi man! thank you for this greate tutorial, but a have one question, its works only in root_path

    but if i need this work all on app pages? how i can do this? thanks again

    Reply |View 0 Replies

    TEDMA410 Oct 14I loved the tutorial. I'm trying to use it in my app. How would you place a user's image in using

    paperclip?

    Reply |View 3 Replies

    VITOR TORRES14 Oct 14

    I've created conversations controller and show view for this controller but ajax calls still returns :

    POST http://localhost:3000/conve... (http://localhost:3000/conversations) 500 (Internal Server

    Error)

    Reply |View 1 Reply

    ANTHONY LIANG28 Oct 14

    Thanks for this awesome tutorial, Joseph! One thing I've been trying to gure out is that current

    if one user messages another user, that user isn't alerted to the message unless they open up the

    window. How could you alert the recipient of the message by popping up the chat window like in

    GChat?

    Reply |View 4 Replies

    MAHMOUD COUDSI10 Nov 14

    http://localhost:3000/conversationshttps://github.com/mcoudsihttps://github.com/liangtfmhttps://github.com/vtorreshttps://github.com/tedma4https://github.com/abdugaliyevhttps://github.com/ashishgoojar
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    17/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 17

    (https://github.com/sigra)

    (https://github.com/jensitus)

    (https://www.facebook.com/Olympian666)

    (https://www.facebook.com/chandrabhushan.yerabatti)

    This is really really amazing! I just ran through the steps and it works perfectly on my computer.

    However, when I try to use it using Safari on my Iphone, the message is sent to my computer and

    the chat window is update but the chat window on Iphone is not updated, I have to reload the

    page for the new message to show on iphone. Any idea why could that be? thanks!

    Reply |View 1 Reply

    IVAN PETROV19 Nov 14

    Amazing guide! Thanks! In chat.js I think it is better to wrap all code after ajax method in `done`

    callback.

    Reply |View 0 Replies

    ENSITUS01 Dec 14Hi Ivan, try in cong/private_pub.yml in development: instead of server: "localhost:3000" server:

    "192.168.1.:3000

    Reply |View 1 Reply

    RYAN DECKER04 Dec 14

    Hey, guys... I'm having a bit of trouble positioning the chatbox in my app. I have a

    footer, and whenever I open the box, it goes below the footer and also creates

    another footer. So...what do I do? :D And one more thing...has anyone gured out

    the way to make the chatbox stay after reload? Thanks.

    Reply |View 1 Reply

    CHANDRABHUSHAN YERABATTI10 Dec 14

    Thank You Michael Teshome Dinku......I put this

    PrivatePub.publish_to("/conversations/#{@conversation}/new",

    "alert('#{@message.body (mailto:#{@message.body)}');") in

    message_contoller creation action but it doesn't work for me. When I

    reload the page then only I'm receiving messages.I'm struggling in this

    .Any help? Thanks.

    Reply |View 1 Reply

    mailto:#%[email protected]://www.facebook.com/chandrabhushan.yerabattihttps://www.facebook.com/Olympian666https://github.com/jensitushttps://github.com/sigra
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    18/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 18

    (https://github.com/gnoshme)

    (https://www.facebook.com/prole.php?id=100005304646099)

    (https://www.facebook.com/rakesh.varma.739978)

    (https://www.facebook.com/ahmed.reza.965)

    (https://www.facebook.com/ahmed.reza.965)

    KEITH HUNNIFORD10 Dec 14

    This is awesome work Joseph! Semi related.. I've been looking at Faye / Heroku mix too and then I found Pubnub.com. Have you

    ever played with Pubnub? Considering using that for something similar to what you've done.

    Reply |View 1 Reply

    YAMINI SHAIK26 Dec 14

    Hi Joseph!Im new to ruby on rails. Its fantastic tutorial. It

    works only on root level.I need to implement same chat app

    for my project using angularjs and rails.I'm unable to return

    the json format for conversation controller and messages

    controller. Can you please help on this.And I unable to

    understand how the routes are working in this app at root

    level.Would you please explain me on this. Thanks

    Reply |View 0 Replies

    RAKESH VARMA29 Dec 14

    Is it work with Rails4 with MongoID?

    Reply |View 0 Replies

    AHMED REZA08 Jan 15

    it worked for me but nothing happen when i click on the send message button....i

    gives a # in the url...am very new with rails but wanna learn this tutorial plz help..

    Reply |View 1 Reply

    AHMED REZA09 Jan 15

    Hey Joseph...tnx a lot buddy but i waana use this on a different app....like my

    problrm is i have everything working gr8 but i jst wanna know wht link to use in

    rgis section of ur lovely tutorial... current_user.id, "data-rip" => user.id %> how to

    https://www.facebook.com/ahmed.reza.965https://www.facebook.com/ahmed.reza.965https://www.facebook.com/rakesh.varma.739978https://www.facebook.com/profile.php?id=100005304646099https://github.com/gnoshme
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    19/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 19

    (https://www.facebook.com/ahmed.reza.965)

    (https://www.facebook.com/prole.php?id=100005304646099)

    (https://www.facebook.com/davchriqui)

    (https://github.com/silvercrow27)

    change that # and by what in the link to send message line....plz buddy plz help na

    plz

    Reply |View 0 Replies

    AHMED REZA10 Jan 15its showing the following error `couldn't nd le 'private_pub'

    Reply |View 1 Reply

    YAMINI SHAIK24 Jan 15

    Hi Joseph....Its a fantastic tutorial for chat app.Is it work with

    mongoid and rails4?

    Reply |View 0 Replies

    DAVID CHRIQUI17 Feb 15

    Thanks for this great tuto :) A little improvement from chat.js instead of doing a get

    request to relative url here : $.get("conversations/" + conversation_id, function (data) {

    $('#chatbox_' + conversation_id).html(data); $("#chatbox_" + conversation_id + "

    .chatboxcontent").scrollTop($("#chatbox_" + conversation_id + " .chatboxcontent")

    [0].scrollHeight); }, "html"); add a "/" just before the conversations resources in order t

    be able to access this resources even if you are manipulating your users resources

    through the view.

    Reply |View 2 Replies

    NORMAN SANTIAGO17 Feb 15

    Thanks for the great tutorial. I just want to ask how to do this in group. I mean I have group

    which is called MyRoom and all of those who are in that room can only see the messages we

    of course you have a uesr more than 2.

    Reply |View 5 Replies

    HMAD HASAN KHAN4 Feb 15

    https://www.facebook.com/er.ahmadhasankhanhttps://github.com/silvercrow27https://www.facebook.com/davchriquihttps://www.facebook.com/profile.php?id=100005304646099https://www.facebook.com/ahmed.reza.965
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    20/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 20

    (https://www.facebook.com/er.ahmadhasankhan)

    (https://github.com/guilhermeap)

    (https://github.com/payoda-suresh)

    (https://github.com/joenilan)

    (https://www.facebook.com/complitech.cspl)

    Hey It's very helpful, Thanks very much, anyone knows how to show online

    users ?

    Reply |View 0 Replies

    EOV GUILHERME DE CARVALHO FILHO05 Mar 15Help me, I got the error, Error: Syntax error, unrecognized expression: #chatbox_ [object

    Object] anyone seen this?

    Reply |View 0 Replies

    SURESH KUMAR V10 Mar 15

    Thanks for this awesome tutorial, Joseph! I'm trying to add support for anonymous userchat.so some advice would be appreciated.

    Reply |View 0 Replies

    OE NILAN11 Mar 15

    This is really awesome. thanks for the tutorial. My only problem is getting it work on another page

    Instead of the send message button using /conversations/# it uses /jobs/conversations/# and give

    me a routing error ActionController::RoutingError (No route matches [POST]

    "/jobs/conversations/2"): when i'm assuming it should just be /conversations/2 Jobs is another

    area of my website. I'm assuming the problem for me is in the $.post("/conversations", { sender_id:

    sender_id, recipient_id: recipient_id }, function (data) { chatBox.chatWith(data.conversation_id);

    line in the users.js le and it's using the directory above it as it's base what do i need to do to get it

    to route correctly any help is MUCH appreciated.

    Reply |View 2 Replies

    COMPLITECH CSPL17 Mar 15

    Hey Joseph,nice tutorial. I have integrated this feature in my app and it is working

    ne.I want to notify user when message arrives.Right now,if chat box open only

    than it will shows conversation.So,is it possible to notify receiver when message

    send by sender?

    Reply |View 1 Reply

    https://www.facebook.com/complitech.csplhttps://github.com/joenilanhttps://github.com/payoda-sureshhttps://github.com/guilhermeaphttps://www.facebook.com/er.ahmadhasankhan
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    21/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 21

    (https://github.com/pranav0073)

    (https://www.facebook.com/piyushgoldenberg)

    (https://github.com/NiVanc)

    (https://www.facebook.com/honza.vosmik)

    (https://www.facebook.com/majeti.subrahmanyaaditya)

    PRANAV PRASHANT20 Mar 15

    I deployed your app on heroku, but found that it works only on http but not over https. Is there a x. Was thinking of adding

    facebook and google+ login

    Reply |View 0 Replies

    TOM GOLDENBERG19 Feb 15

    I implemented all of the provided code, but the "message" buttons do nothing

    when clicked. If the a href leads to "#", I don't see how to get it to work, and am

    not familiar with JQuery. Any suggestions? Thanks!

    Reply |View 2 Replies

    NICO VANCAMP24 Mar 15

    Could someone help me on how to count the number of unread messages and display that in the

    your conversations list?

    Reply |View 0 Replies

    HONZA VOSMIK28 Mar 15

    Great tutorial and really helpful to me! One question. How would you make to have

    the chat window in same state as it was when you refresh a page? For example, if

    one window is opened, another minimized, you refresh the page and it's still the

    same. Thank you once again! :)

    Reply |View 1 Reply

    DITYA MAJETI09 Apr 15

    HI, JOSEPH NDUNGU i have a problem on calling " /conversations"

    controller in user.js its is routs to index of conversations controller

    hich is not dened. but i changed to /conversations/create and i just

    create conversations/create.html.erb but it is showing 404 i dont know

    hy popup of message box is not opened

    https://www.facebook.com/majeti.subrahmanyaadityahttps://www.facebook.com/honza.vosmikhttps://github.com/NiVanchttps://www.facebook.com/piyushgoldenberghttps://github.com/pranav0073
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    22/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 22

    (https://www.facebook.com/aulia.sabril)

    (https://www.facebook.com/ybhavkale)

    (https://www.facebook.com/majeti.subrahmanyaaditya)

    (https://www.facebook.com/benjyforrest)

    Reply |View 0 Replies

    AULIA SABRIL26 Mar 15

    Thanks; great tutorial, Joseph if you don't mind, how can we add video or talk withmicrophone in this chat??

    Reply |View 1 Reply

    VAIBHAV KALE08 Apr 15

    great tutorial Joseph. Thank you.I am facing some issue here. Once I have setup the

    entire code based on what is given here nothing seems to be happening on clicking the

    send message link. I am getting the below mentioned error which i copied from the

    console. Any suggestion would be of great help. Thanks send jQuery.extend.ajax

    jQuery.(anonymous function) chatBox.createChatBox chatBox.chatWith (anonymous

    function) re self.reWith done callback

    Reply |View 2 Replies

    DITYA MAJETI09 Apr 15

    HI, JOSEPH NDUNGU i have a problem on calling " /conversations"

    controller in user.js its is routs to index of conversations controllerhich is not dened. but i changed to /conversations/create and i just

    create conversations/create.html.erb but it is showing 404 i dont know

    hy popup of message box is not opened

    Reply |View 3 Replies

    BEN FORREST11 Apr 15

    So useful! Nice one I wondered if anyone managed to get ofine caching of messageworking eg gmail chat when the recipient is ofine it message is stored until they

    login

    Reply |View 0 Replies

    DAVID CHRIQUI12 Apr 15

    https://www.facebook.com/davchriquihttps://www.facebook.com/benjyforresthttps://www.facebook.com/majeti.subrahmanyaadityahttps://www.facebook.com/ybhavkalehttps://www.facebook.com/aulia.sabril
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    23/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 23

    (https://www.facebook.com/davchriqui)

    (https://github.com/Mubashwer)

    (https://github.com/bbetter)

    (https://github.com/pvijayror)

    (https://github.com/m-sakthivel)

    Hi Joseph, Thank you very much for your very useful blog tutorials. I've successfully

    implemented your gmail like chat into my app but I encounter some troubles setting u

    faye in a production environment with heroku. I've used this project to get private_pub

    Faye running on heroku : github.com/iamnan/heroku_private_pub This is not working

    with my project as well as your chat app. Would it be possible to get the code (through

    github) you've deployed to heroku in order to get running the Faye server you use ?

    Thank you

    Reply |View 0 Replies

    MUBASHWER SALMAN KHURSHID14 Apr 15

    Hi.. i got it working but the messages do not appear real-time.. i have to refresh the whole

    page.. and there are no error messages in the console log so I don't know what im doing wrong

    Reply |View 2 Replies

    01 May 15

    do we need UserController, i followed this guide but now am getting error uninitialized constant

    UsersController

    Reply |View 1 Reply

    PVIJAYROR09 May 15

    Thank you working ne... but not 100% useful. Only problem: "if one user messages another use

    that user isn't alerted to the message unless they open up the window. How could you alert the

    recipient of the message by popping up the chat window like in GChat?"

    Reply |View 0 Replies

    M-SAKTHIVEL1 May 15

    Hi I tried this out, I have a problem here when i post a message am getting template missing

    error since we are using create.js.erb but it's looking for html le i don know hw to x this any

    one can help me pls.....

    Reply |View 1 Reply

    https://github.com/m-sakthivelhttps://github.com/pvijayrorhttps://github.com/bbetterhttps://github.com/Mubashwerhttps://www.facebook.com/davchriqui
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    24/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 24

    (https://github.com/Anyatskar)

    (https://github.com/Anyatskar)

    (https://github.com/sarahgupta022)

    (https://github.com/gakshay)

    ANNA21 May 15

    Hello! Im also having the same problem as MUBASHWER SALMAN KHURSHID (the chat does not update in real-time, but the

    sent messages of both parties exist when I refresh the whole webpage). I cloned JOSEPH NDUNGUs exact app from Github, bu

    it has the same issue as described above. Clues?

    Reply |View 5 Replies

    ANNA21 May 15

    Otherwise, the url in private_pub does not work. Is this not correct (obviously not, since its not

    working for me, but what else should go there): server: "http://localhost:9292/faye/...

    (http://localhost:9292/faye/faye)?

    Reply |View 1 Reply

    SARAHGUPTA02210 Jul 15

    Hi Joseph, Thank you very much for your very useful blog tutorials.I am trying to put "gma

    like chat" in my existing code. but i receive error in my web page: Unknown action(The

    action 'index' could not be found for ConversationsController) from my Conversations

    Controller.rb . i try to put def index but after that i get another error: Template is missingMissing template conversations/index, application/index with {:locale=>[:en], :formats=>

    [:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml,

    :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb,

    :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: *

    "C:/Users/Arvind/project/book/app/views" *

    "c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/twitter-bootstrap-rails-

    3.2.0/app/views" * "c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/devise-

    3.5.1/app/views" Now i really stuck in my app and i don't know how to get over with it. can

    u plz plz help me. My git repository at: https://github.com/sarahgup...

    (https://github.com/sarahgupta022/book.git)

    Reply |View 1 Reply

    AKSHAY GUPTA11 Jul 15

    Audio was also not getting automatically played. So download any notication sound on internet

    and put it in public directory naming notify.mp3 and it will start working

    https://github.com/sarahgupta022/book.githttp://localhost:9292/faye/fayehttps://github.com/gakshayhttps://github.com/sarahgupta022https://github.com/Anyatskarhttps://github.com/Anyatskar
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    25/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 25

    (https://github.com/pvijayror)

    (https://github.com/sanjusoftware)

    (https://github.com/pvijayror)

    (https://github.com/anilsingh826)

    (https://github.com/anilsingh826)

    Reply |View 0 Replies

    PVIJAYROR18 Jul 15

    Hi Akshay Gupta, Please let me know can you describe to me automatically opening the chatwindow

    Reply |View 1 Reply

    SANJEEV KUMAR MISHRA05 Jun 15

    Hi Joseph, this is by far one of the most useful tutorials I have ever found in the recent past

    thank you so much for this. BTW, I am also one of those who is looking for the following 2

    things: #1. Open the chat window of the other person if he's online automatically: If I had

    been in conversation with another user already and had just closed it for some reason, the

    chat window does popup for me if that same user sends me another message. But nothing

    happens if someone has just initiated a conversation. Would be great help to get this one in

    I know a lot of people have already requested for it, so I hope you help us around this. Or

    just gimme some idea/hint :) #2. Detecting if a user if online of ofine and based on that

    showing some visual on the chat window / hide the send message button etc

    Reply |View 3 Replies

    PVIJAYROR10 Jun 15Hi Joseph, Thank you for update :) Plase x ASAP automatically opening the chat window

    Reply |View 1 Reply

    ANIL SINGH23 Jun 15

    Hi Joseph, Thanks for this nice tutorial, I am facing one issue with it. Started GET "/faye.js" fo

    127.0.0.1 at 2015-06-23 15:27:12 +0530 ActionController::RoutingError (No route matche[GET] "/faye.js"): Could you please help to x this issue. Thanks in Advance :)

    Reply |View 0 Replies

    ANIL SINGH23 Jun 15

    I have xed this issue.

    https://github.com/anilsingh826https://github.com/anilsingh826https://github.com/pvijayrorhttps://github.com/sanjusoftwarehttps://github.com/pvijayror
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    26/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 26

    (https://github.com/gakshay)

    (https://github.com/moussasarr)

    (https://github.com/balla121)

    (https://github.com/siva23)

    (https://github.com/gakshay)

    Reply |View 0 Replies

    AKSHAY GUPTA11 Jul 15

    I have posted my solution on above thread...for opening the chat box automatically for anotheronline user

    Reply |View 1 Reply

    MOUSSASARR20 Aug 15

    Great tutorial. Now I am wondering if you can from here have a inbox, trash and sent that will

    look like the one you made with mailboxer but using only private pub and thin. Can you go fro

    here to an inbox system in coordination with the chat messages ? If so, how ?

    Reply |View 1 Reply

    WEIJIE GAO27 Aug 15

    hey! I wrote a short blog post on how to set up the private pub messenger for https/ssl. There wa

    a mistake in the private pub read me for this issue. check it out! http://www.weijieworld.com/...

    (http://www.weijieworld.com/how-to-setup-your-rails-private-pub-messenger-for-httpsssl/)

    Reply |View 0 Replies

    TEKI.V.SIVARAMAKRISHNA14 Sep 15

    It took me two days to understand the process of faye push server and after following AKSHAY

    GUPTA's reply .... now I can successfully alert the other user when someone initiates the

    conversation. Pretty thanks to akshay and joseph ndungu.

    Reply |View 0 Replies

    AKSHAY GUPTA14 Sep 15

    Thanks Teki.. good it was helpful...

    Reply |View 0 Replies

    http://www.weijieworld.com/how-to-setup-your-rails-private-pub-messenger-for-httpsssl/https://github.com/gakshayhttps://github.com/siva23https://github.com/balla121https://github.com/moussasarrhttps://github.com/gakshay
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    27/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 27

    (https://github.com/brunosiqueira)

    (https://github.com/xbits)

    (https://github.com/RealSilo)

    (https://github.com/bangoc123)

    (https://github.com/lmahendra)

    BRUNO SIQUEIRA27 Sep 15

    Hey, Joseph! Thank you for the great tutorial! private_pub had been outdated for more tha

    3 years. Do you know if there another gem option? Thanks?

    Reply |View 0 Replies

    XBITS23 Sep 15

    Had a problem which took me a while to gure out. Faye channels were not being subscribed due to

    GET parameters in the channel name EX: "/conversation/3?locale=pt". Faye would then send a

    response with error 405 invalid channel. In case anyone has the same problem simply add ``ruby

    your_channel_name.split('?').rst ``

    Reply |View 0 Replies

    SZILARD MAGYAR05 Oct 15

    Hey! Awesome tut! I am pretty new to rails. Could sby tell me how I could embed the chat functio

    into rails? So when I click on a user show page the chat is already there as part of the rails app,

    without using any JS to open the chat window.

    Reply |View 0 Replies

    BANGOC12324 Oct 15

    How can i display this chat at any page in my app? Please!!!

    Reply |View 2 Replies

    LMAHENDRA05 Oct 15

    Hi Joseph, thanks for an awesome tutorial. I could make it work on my localhost successfully.

    However, when I deployed it to my dev server running nginx on AWS having linux AMI, I had to

    reload the page to see new messages. I added server IP in private_pub.yml and updated

    nginx.conf with /faye url, also opened 9292 port from aws console. but not able to resolve the

    following error Errno::ETIMEDOUT in Spree::Messages#create Showing

    app/views/spree/messages/create.js.erb where line #1 raised: Connection timed out -

    connect(2) for "54.xx.xx.171" port 9292

    Reply |View 1 Reply

    https://github.com/lmahendrahttps://github.com/bangoc123https://github.com/RealSilohttps://github.com/xbitshttps://github.com/brunosiqueira
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    28/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 28

    (https://github.com/lmahendra)

    (https://github.com/maxmeira)

    (https://github.com/zelLimbo)

    (https://github.com/leongg)

    (https://github.com/rmatejacabello)

    LMAHENDRA05 Oct 15

    adding //= require private_pub to application.js resolved my issue, god, how did I missed that?

    Now I want to add API functionality to the chat for building android / ios application. If anyone

    already did that, please guide me.

    Reply |View 0 Replies

    MAXMEIRA29 Oct 15

    Excellent tutorial dear Joseph! Need to put the button "Send Message" on views users / prole

    and so he opens the message box for that particular user, please guide me how to do this! Be

    with a description or sample, is of paramount importance! Congratulations for the work, very

    grateful!

    Reply |View 0 Replies

    ZELLIMBO29 Oct 15

    Fantastic writing . I was fascinated by the info . Does anyone know if my company might be able

    to obtain a blank 2012 CDC CDC/NCHS-6205-1 version to type on ?

    Reply |View 0 Replies

    LEONGG31 Oct 15

    Hi Joseph, I got an error: Can't verify CSRF token authenticity

    ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken: [...] Hav

    you seen that error? do you know how can I x it? Many thanks!

    Reply |View 2 Replies

    RMATEJACABELLO28 Nov 15

    For those who want to do an auto popup window when someone sends you a message, I

    just realized there is a commit request in the Github brung from the user Mubashwer that

    xes that. Check the les changes here and include it in your code:

    https://github.com/Joseph-N... (https://github.com/Joseph-N/chatty/pull/4/les) Basically

    this user created a channel right after loading your application that subscribes to a

    https://github.com/Joseph-N/chatty/pull/4/fileshttps://github.com/rmatejacabellohttps://github.com/leongghttps://github.com/zelLimbohttps://github.com/maxmeirahttps://github.com/lmahendra
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    29/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 29

    (https://github.com/Edward-Teach)

    (https://github.com/tomcaridi)

    (https://github.com/lmahendra)

    (https://github.com/pragatisigma)

    "notications" channel. This is also very helpful to know when the user is online or ofine

    since that channel is being subscribed when you enter the app. You can take a look at this

    discussion here from przbadu (https://github.com/ryanb/pr...

    (https://github.com/ryanb/private_pub/issues/110)) where he uses app.bind(:subscribe)

    and app.bind(:unsubscribe) events that re up when the user subscribes to a channel. You

    can play with the code and detect every time an user subscribes to the notication channe

    which would be the same as knowing when the user comes online or ofine. With this, I

    think this chat is 100% operative and useful for any application. (Would be awesome to

    have also the "Typing.." message)

    Reply |View 0 Replies

    AN01 Dec 15

    Hello Joseph, I don't know if there was an update but at the moment I receive a 500 interna

    error when I try to send a message, as well as in your Demo > http://railschattyapp.herok...

    (http://railschattyapp.herokuapp.com/). Any tips? Thanxs .-)

    Reply |View 1 Reply

    TOM CARIDI16 Dec 15

    Hey Joseph, rst of all thanks for the super detailed tutorial! I implemented everything as you

    have but when clicking on the Send Message button next to the users, nothing happens. My cod

    is exactly the same as yours so I'm wondering if it's a Ruby or Rails version issue since you built

    this a while ago. Any advice?

    Reply |View 2 Replies

    LMAHENDRA18 Dec 15

    can you provide more details as in what's the error you are getting and RoR version?

    Reply |View 4 Replies

    PRAGATISIGMA28 Dec 15

    Hi Joseph, Amazing tutorial. But for a receiver to receive alert i used "/conversations/#

    {@conversati... (mailto:conversations/#{@conversation.id)}/new" and it is not working. Also,

    want to popup remain open for all the pages even i refresh the page like facebook. Any help?

    Reply |View 0 Replies

    mailto:conversations/#%[email protected]://railschattyapp.herokuapp.com/https://github.com/ryanb/private_pub/issues/110https://github.com/pragatisigmahttps://github.com/lmahendrahttps://github.com/tomcaridihttps://github.com/Edward-Teach
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    30/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 30

    (https://github.com/soreddysirish)

    (https://github.com/stefanocdn)

    (https://github.com/ssoulless)

    (https://github.com/ssoulless)

    (https://github.com/khavq)

    SIREESH06 Jan 16

    HI joseph this is tutorial thank you. please solve my issue when ever click on send a message

    link at that time only the channel is subscribing .if user is not click on the button he is not go

    any message.

    Reply |View 0 Replies

    STEPHANE CEDRONI07 Jan 16

    Anybody tried to add an index action on the conversations? I am trying to display the list of

    conversation like gmail. But can't nd a way to efciently fetch the interlocutors with a JOIN

    (otherwise this is doing N+1 query). Anybody found a solution for that?

    Reply |View 0 Replies

    SEBASTIAN VELANDIA11 Jan 16

    Great tutorial, now how to add le attachments to the messages?

    Reply |View 0 Replies

    SEBASTIAN VELANDIA11 Jan 16

    By the way, the demo app is not working!!

    Reply |View 1 Reply

    KHAVQ13 Jan 16

    his is really nice tutorial, I was making it work on local (computer A) but when I using anothercomputer (computer B) to access 10.1.0.125:8520 so I got a problem. When I type on B so A still can

    received the message but chat list on B is not update and when I type on A nothing happen. Any

    ideas can help me ? Thanks for your time.

    Reply |View 1 Reply

    https://github.com/khavqhttps://github.com/ssoullesshttps://github.com/ssoullesshttps://github.com/stefanocdnhttps://github.com/soreddysirish
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    31/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 31

    (https://github.com/keyeh)

    (https://github.com/harshad26)

    (https://github.com/TylerTaylor)

    (https://github.com/owuor91)

    (https://github.com/owuor91)

    KEVIN YEH04 Mar 16

    Danthes is a better better gem to use as private_pub is no longer maintained. https://github.com/dotpromo...

    (https://github.com/dotpromo/danthes)

    Reply |View 0 Replies

    HARSHAD2608 Apr 16

    I setup it but just open chatbox. Not received message to another person. Also when i write

    message and post but not show in chat window. Its show only after reload page. What i missed

    here. Any one have a idea. Thanks

    Reply |View 0 Replies

    TYLERTAYLOR15 Feb 16

    The message pop up is not working for me, it's pointing to '/#' and at this point I have cloned

    and copied your code.

    Reply |View 2 Replies

    OHN OWUOR23 Feb 16

    Mine works well, nally. \o/. But it only works in the index page of the app. I would like to have it

    work from another resource e.g localhost:3000/proles/5/conversations/1 instead of

    localhost:3000/conversations/1 . Any Idea how to go about this?

    Reply |View 0 Replies

    OHN OWUOR25 Feb 16

    gured it out. will make a pull request soon

    Reply |View 0 Replies

    https://github.com/dotpromo/dantheshttps://github.com/owuor91https://github.com/owuor91https://github.com/TylerTaylorhttps://github.com/harshad26https://github.com/keyeh
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    32/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 32

    (https://github.com/harshad26)

    (https://github.com/kinyodan)

    (https://github.com/kinyodan)

    (https://github.com/dsCloud)

    (https://github.com/dsCloud)

    HARSHAD2608 Apr 16

    messages save into database but not show instant. Its show after reload window. Why? any one have.

    Reply |View 1 Reply

    KINYODAN23 Apr 16

    Please help i need to know which part of the code requires a user to be signed in in order to

    display the pop up message box, since i need to change it i am rendering the message box on a

    separate controller view i have already done this but i have multiple models for devise guest and

    user i need the user authentication removed i have removed the authenticate user from the

    message and conversations controllers already but the message box only displays if user is signe

    in

    Reply |View 0 Replies

    KINYODAN23 Apr 16

    oh managed to get it working

    Reply |View 0 Replies

    DSCLOUD30 Apr 16

    An amazing tutorial Joseph. Thanks a lot!!! However, while trying to run...rails g

    private_pub:install.... I encounter this error Could not nd generator 'private_pub:install'. Maybe

    you meant 'devise:install', 'responders:install' or 'integration_test'. So i could not proceed,

    obviously. Any assistance/guide on what to do will be highly appreciate.

    Reply |View 1 Reply

    DSCLOUD30 Apr 16

    I am developing on Nitrous environment,

    Reply |View 0 Replies

    https://github.com/dsCloudhttps://github.com/dsCloudhttps://github.com/kinyodanhttps://github.com/kinyodanhttps://github.com/harshad26
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    33/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails 33

    Log in to leave a comment...

    Sign in with Github (/users/auth/github)

    Sign in with Facebook (/users/auth/facebook)

    LATEST TUTORIALS___

    PRIVATE INBOX SYSTEM IN RAILS WITH MAILBOXER NEWRails05 Apr 15

    ntroduction It's been quite a while since my last tutorial and since then I've recieved alot of requests by email to implement a priva

    messaging system ...

    AJAX SORTABLE LISTS RAILS 4Ajax

    22 Dec 14

    With me, is a simple to-do list application where users can create dummy to-do lists and displays them in card-like form just like in

    Trello. We want to e...

    MANAGING ENV VARIABLES IN RAILSRails

    20 Oct 14

    Often when developing Rails applications, you will nd a need to setup a couple of environment variables to store secure informati

    such as passwords, a...

    GMAIL LIKE CHAT APPLICATION IN RUBY ON RAILSChat

    30 Jul 14

    ntroduction We are all fond of the Gmail and Facebook inline chat modules. About a week ago, I came across a tutorial on how to

    replicate hangouts chat...

    http://josephndungu.com/users/auth/facebookhttp://josephndungu.com/users/auth/github
  • 7/26/2019 Gmail Like Chat Application in Ruby on Rails _ Joseph Ndungu

    34/34

    5/16/2016 Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

    Load more (/tutorials/more)

    FAST AUTOCOMPLETE SEARCH TERMS - RAILSSearch

    13 Jul 14

    ntroduction In many cases you nd that you need to introduce a global search in your rails application i.e. search multiple models a

    go using one form...

    http://josephndungu.com/tutorials/more