You are on page 1of 34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

JosephNdungu(/)
PROJECTS
(/PROJECTS)

TUTORIALS
(/TUTORIALS)

CONTACT
(/CONTACT)

(HTTPS://WWW.FACEBOOK.COM/PROFILE.PHP?
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 AdWords


How to Boost Your PPC Performance. Download Your Free Guide Now!

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

1/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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 thought to
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 chat
in a Rails application. I wanted to have a near-realtime kind of chat functionality and in my research I found out that the
publish/subscribe model was the most ef cient, 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://postranklabs.github.com/goliath/) are all great solutions but it would be super awesome to continue using Rails for our application's logic and
also have the bene ts of some kind of asynchronous event handling with publishing and subscribing when we need that. To achieve
this, we will use Private Pub Gem (https://github.com/ryanb/private_pub) which is built on top of Faye (http://faye.jcoglan.com/) and
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 conversation
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. On
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 logic
is very simple. A user will have many conversations and a conversation will have many messages. Here is the relationship diagram

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

2/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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

classCreateConversations<ActiveRecord::Migration

defchange

create_table:conversationsdo|t|

t.integer:sender_id

t.integer:recipient_id

6
7

t.timestamps

end

10

add_index:conversations,:sender_id

11

add_index:conversations,:recipient_id

12

end

13

end

view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/create_conversations.rb)


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 the user_id column in our conversation table, we must
explicitly tell rails which foreign key to use.
1

classUser<ActiveRecord::Base

2
3

devise:database_authenticatable,:registerable,

:recoverable,:rememberable,:trackable,:validatable

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

3/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

5
6

has_many:conversations,:foreign_key=>:sender_id

end

user.rb

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

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

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

classConversation<ActiveRecord::Base

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

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

4
5

has_many:messages,dependent::destroy

6
7

validates_uniqueness_of:sender_id,:scope=>:recipient_id

8
9

scope:involving,>(user)do

10

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

11

end

12
13

scope:between,>(sender_id,recipient_id)do

14

where("(conversations.sender_id=?ANDconversations.recipient_id=?)OR(conversations.sender_id=?ANDconversat

15

end

16

end

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


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 scope
of the recipeint_id. What this does is that it ensures that the sender_id and 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

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

4/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

classMessage<ActiveRecord::Base

belongs_to:conversation

belongs_to:user

4
5

validates_presence_of:body,:conversation_id,:user_id

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

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 page,
we will add a send message button along each user's name. The button will hold two 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 returns
conversation_id 3, we will request the html page at conversations/3. We will then append this conversation data in our home page in
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. The data-sid attribute will
hold our current users' id while data-rip attribute will hold the recipient's id. We will send this values through an ajax request to our
server which should create a conversation if necessary.
index.html.erb
1

<%@users.each_with_indexdo|user,index|%>

<tr>

<td><%=index+=1%></td>

<td><%=user.name%></td>

<td>

<%=link_to"SendMessage","#",class:"btnbtnsuccessbtnxsstartconversation",

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

</td>

</tr>

10

<%end%>

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


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 create a
le chat.js inside our javascripts folder and paste the contents of chat.js (https://gist.github.com/JosephN/d1413cc5822efa416175# le-chat-js). This le has all the functions we will need when creating chatboxes in our home page and
also communicating with our rails app.
Let's not forget to add this le to our javascript manifest le.

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

5/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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 our chat.js le
users.js
1

varready=function(){

2
3

/**

*Whenthesendmessagelinkonourhomepageisclicked

*sendanajaxrequesttoourrailsappwiththesender_idand

*recipient_id

*/

8
9
10

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

11
12

varsender_id=$(this).data('sid');

13

varrecipient_id=$(this).data('rip');

14
15

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

16

chatBox.chatWith(data.conversation_id);

17

});

18

});

19
20

/**

21

*Usedtominimizethechatbox

22

*/

23
24

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

25

e.preventDefault();

26
27

varid=$(this).data('cid');

28

chatBox.toggleChatBoxGrowth(id);

29

});

30
31

/**

32

*Usedtoclosethechatbox

33

*/

34
35

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

36

e.preventDefault();

37
38

varid=$(this).data('cid');

39

chatBox.close(id);

40

});

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

6/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

41
42
43

/**

44

*Listenonkeypress'inourchattextareaandcallthe

45

*chatInputKeyinchat.jsforinspection

46

*/

47
48

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

49
50

varid=$(this).data('cid');

51

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

52

});

53
54

/**

55

*Whenaconversationlinkisclickedshowuptherespective

56

*conversationchatbox

57

*/

58
59

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

60

e.preventDefault();

61
62

varconversation_id=$(this).data('cid');

63

chatBox.chatWith(conversation_id);

64

});

65
66
67

68
69

$(document).ready(ready);

70

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

users.js view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/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 our conversations 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, the create and the show actions.
conversations_controller.rb
1

classConversationsController<ApplicationController

before_filter:authenticate_user!

3
4

layoutfalse

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

7/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

defcreate

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

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

else

10

@conversation=Conversation.create!(conversation_params)

11

end

12
13

renderjson:{conversation_id:@conversation.id}

14

end

15
16

defshow

17

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

18

@reciever=interlocutor(@conversation)

19

@messages=@conversation.messages

20

@message=Message.new

21

end

22
23

private

24

defconversation_params

25

params.permit(:sender_id,:recipient_id)

26

end

27
28

definterlocutor(conversation)

29

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

30

end

31

end

view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/conversations_controller.rb)


conversations_controller.rb (https://gist.github.com/Joseph-N/d1413cc5822efa416175#file-conversations_controller-rb) hosted with by
GitHub (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 the sender_id and recipient_id. Recall our between
scope we de ned 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 a
json response with the id of the conversation.
Our show action is also pretty standard, after getting the conversation with the speci c id, (note this id is sent by our javascript code
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 Gem le and run bundle. Installing it will also install Faye and
its dependencies. We will also include the thin gem as we will be using it to serve Faye.
gem 'private_pub'
gem 'thin'

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

8/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

Next up, we need to run the generator provided by private_pub to generate the con guration 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 last
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

<divclass="chatboxhead">

<divclass="chatboxtitle">

<iclass="fafacomments"></i>

4
5

<h1><%=@reciever.name%></h1>

</div>

<divclass="chatboxoptions">

<%=link_to"<iclass='fafaminus'></i>".html_safe,"#",class:"toggleChatBox","datacid"=>@conversation

&nbsp;&nbsp;

10

<%=link_to"<iclass='fafatimes'></i>".html_safe,"#",class:"closeChat","datacid"=>@conversation.id

11

</div>

12

<brclear="all"/>

13

</div>

14

<divclass="chatboxcontent">

15

<%if@messages.any?%>

16

<%=render@messages%>

17

<%end%>

18

</div>

19

<divclass="chatboxinput">

20

<%=form_for([@conversation,@message],:remote=>true,:html=>{id:"conversation_form_#{@conversation.id}"})

21

<%=f.text_area:body,class:"chatboxtextarea","datacid"=>@conversation.id%>

22

<%end%>

23

</div>

24
25

<%=subscribe_toconversation_path(@conversation)%>

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


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

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

9/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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 noti cations to this channel from our controller. We do this by calling subscribe_to and 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 notice
that our form will always be submitting via ajax due to the remote option.
Our last controller is the messages controller.
$ rails g controller messages

messages_controller.rb
1

classMessagesController<ApplicationController

before_filter:authenticate_user!

3
4

defcreate

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

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

@message.user_id=current_user.id

@message.save!

9
10

@path=conversation_path(@conversation)

11

end

12
13

private

14
15

defmessage_params

16

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

17

end

18

end

view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_controller.rb)


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 the conversation's path in the @path instance variable. We
will use this path to publish push noti cations to our view. Remember we suscribed to the same url in our conversation's show view.
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

<%publish_to@pathdo%>

varid="<%=@conversation.id%>";

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

varsender_id="<%=@message.user.id%>";

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

6
7

chatbox.append("<%=jrender(partial:@message)%>");

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

10/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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

9
10

if(sender_id!=reciever_id){

11

12

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

13

14

chatBox.notify();

15

16

<%end%>

chatBox.chatWith(id);
chatbox.scrollTop(chatbox[0].scrollHeight);

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


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 in
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 noti cations. A simple way around this is to create a meta tag in
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

<metacontent='<%=user_signed_in??current_user.id:""%>'name='userid'/>

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


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 a message partial. We have not created that yet. In our messages folder
_message.html.erb
1

<liclass="<%=self_or_other(message)%>">

<divclass="avatar">

<imgsrc="http://placehold.it/50x50"/>

</div>

<divclass="chatboxmessagecontent">

<p><%=message.body%></p>

<timedatetime="<%=message.created_at%>"title="<%=message.created_at.strftime("%d%b%Yat%I:%M%p")%>">

<%=message_interlocutor(message).name%><%=message.created_at.strftime("%H:%M%p")%>

</time>

10

</div>

11

</li>

view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/_message.html.erb)


_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 de ne them. In our messages_helper le
messages_helper.rb

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

11/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

moduleMessagesHelper

defself_or_other(message)

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

end

5
6

defmessage_interlocutor(message)

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

end

end

10
view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/messages_helper.rb)
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 de ned their respective routes. Let's add
them right away
routes.rb
1

Rails.application.routes.drawdo

2
3

devise_for:users

4
5

authenticated:userdo

root'users#index'

end

8
9

unauthenticated:userdo

10

devise_scope:userdo

11

get"/"=>"devise/sessions#new"

12

end

13

end

14
15

resources:conversationsdo

16

resources:messages

17

end

18

end

routes.rb view raw (https://gist.github.com/Joseph-N/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/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 le chat.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

<!DOCTYPEhtml>

<htmllang="en">

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

12/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

<head>

<metacharset="utf8">

<metahttpequiv="XUACompatible"content="IE=edge">

<metaname="viewport"content="width=devicewidth,initialscale=1">

<metaname="description"content="">

<metaname="author"content="">

<metacontent='<%=user_signed_in??current_user.id:""%>'name='userid'/>

10
11

<title>Chatty</title>

12

<%=stylesheet_link_tag'application',media:'all','dataturbolinkstrack'=>true%>

13

<%=stylesheet_link_tag'//maxcdn.bootstrapcdn.com/fontawesome/4.1.0/css/fontawesome.min.css'%>

14

<%=javascript_include_tag'application','dataturbolinkstrack'=>true%>

15

<%=csrf_meta_tags%>

16
17

<!shivhere>

18
19

</head>

20
21

<body>

22
23

<%=render'layouts/nav'%>

24
25

<divclass="container">

26

<!flashmessageshere>

27

<%=yield%>

28

</div>

29

<audioid="chatAudio"><sourcesrc="/sounds/notification.mp3"type="audio/mpeg"></audio>

30

</body>

31
32

</html>

view raw (https://gist.github.com/JosephN/d1413cc5822efa416175/raw/ea3fc8d0def729ef724a00f3fd829b06d759fb98/application.html.erb)


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 handling near-realtime instant
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. On
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

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

13/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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 working
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 NDUNGU

I'm a passionate web developer specializing in ruby on rails. This is my notepad where I share code snippets and
tutorials 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)

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

14/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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


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

___

85 COMMENTS
THOMAS QUIROGA
06 Aug 14

(https://github.com/thomasquiroga) Huge thanks, it's really a good work!


Reply | View 0 Replies

SANTHU46
11 Aug 14

(https://github.com/santhu46) 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

KUMRZZ
20 Aug 14

(https://github.com/kumrzz) love it, nice to see a relevant and recent app on privatepub: thanks! I was tearing my hair out failing
to get such an app out ... one suggestion though, I think its best to have more than one user setup in
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

LAWRENCEN
04 Sep 14

(https://github.com/lawrenceN) This is a nice idea man and very clear writing.


Reply | View 0 Replies

ASHISHGOOJAR
23 Sep 14

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

15/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/ashishgoojar) 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

ABDUGALIYEV
07 Oct 14

(https://github.com/abdugaliyev) 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

TEDMA4
10 Oct 14

(https://github.com/tedma4) I 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 TORRES
14 Oct 14

(https://github.com/vtorres) 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 LIANG
28 Oct 14

(https://github.com/liangtfm) Thanks for this awesome tutorial, Joseph! One thing I've been trying to gure out is that currently
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 COUDSI
10 Nov 14
(https://github.com/mcoudsi)

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

16/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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 PETROV
19 Nov 14

(https://github.com/sigra) 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

JENSITUS
01 Dec 14

(https://github.com/jensitus) Hi Ivan, try in con g/private_pub.yml in development: instead of server: "localhost:3000" server:
"192.168.1.:3000
Reply | View 1 Reply

RYAN DECKER
04 Dec 14

(https://www.facebook.com/Olympian666) 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 YERABATTI
10 Dec 14

(https://www.facebook.com/chandrabhushan.yerabatti) 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

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

17/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/gnoshme)

KEITH HUNNIFORD
10 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 SHAIK
26 Dec 14

(https://www.facebook.com/pro le.php?id=100005304646099) 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 VARMA
29 Dec 14

(https://www.facebook.com/rakesh.varma.739978) Is it work with Rails4 with MongoID?


Reply | View 0 Replies

AHMED REZA
08 Jan 15

(https://www.facebook.com/ahmed.reza.965) it worked for me but nothing happen when i click on the send message button....it
gives a # in the url...am very new with rails but wanna learn this tutorial plz help...
Reply | View 1 Reply

AHMED REZA
09 Jan 15

(https://www.facebook.com/ahmed.reza.965) 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

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

18/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

change that # and by what in the link to send message line....plz buddy plz help na
plz
Reply | View 0 Replies

AHMED REZA
10 Jan 15

(https://www.facebook.com/ahmed.reza.965) its showing the following error `couldn't nd le 'private_pub'


Reply | View 1 Reply

YAMINI SHAIK
24 Jan 15

(https://www.facebook.com/pro le.php?id=100005304646099) Hi Joseph....Its a fantastic tutorial for chat app.Is it work with
mongoid and rails4?
Reply | View 0 Replies

DAVID CHRIQUI
17 Feb 15

(https://www.facebook.com/davchriqui) 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 to
be able to access this resources even if you are manipulating your users resources
through the view.
Reply | View 2 Replies

NORMAN SANTIAGO
17 Feb 15

(https://github.com/silvercrow27) 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 well
of course you have a uesr more than 2.
Reply | View 5 Replies

AHMAD HASAN KHAN


24 Feb 15

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

19/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://www.facebook.com/er.ahmadhasankhan) Hey It's very helpful, Thanks very much, anyone knows how to show online
users ?
Reply | View 0 Replies

JEOV GUILHERME DE CARVALHO FILHO


05 Mar 15

(https://github.com/guilhermeap) Help me, I got the error, Error: Syntax error, unrecognized expression: #chatbox_ [object
Object] anyone seen this?
Reply | View 0 Replies

SURESH KUMAR V
10 Mar 15

(https://github.com/payoda-suresh) Thanks for this awesome tutorial, Joseph! I'm trying to add support for anonymous user
chat.so some advice would be appreciated.
Reply | View 0 Replies

JOE NILAN
11 Mar 15

(https://github.com/joenilan) 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 gives
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 CSPL
17 Mar 15

(https://www.facebook.com/complitech.cspl) 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

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

20/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/pranav0073)

PRANAV PRASHANT
20 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 GOLDENBERG
19 Feb 15

(https://www.facebook.com/piyushgoldenberg) 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 VANCAMP
24 Mar 15

(https://github.com/NiVanc) 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 VOSMIK
28 Mar 15

(https://www.facebook.com/honza.vosmik) 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

ADITYA MAJETI
09 Apr 15

(https://www.facebook.com/majeti.subrahmanyaaditya) HI, JOSEPH NDUNGU i have a problem on calling " /conversations"


controller in user.js its is routs to index of conversations controller
which is not de ned. but i changed to /conversations/create and i just
create conversations/create.html.erb but it is showing 404 i dont know
why popup of message box is not opened

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

21/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

Reply | View 0 Replies

AULIA SABRIL
26 Mar 15

(https://www.facebook.com/aulia.sabril) Thanks; great tutorial, Joseph if you don't mind, how can we add video or talk with
microphone in this chat??
Reply | View 1 Reply

VAIBHAV KALE
08 Apr 15

(https://www.facebook.com/ybhavkale) 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

ADITYA MAJETI
09 Apr 15

(https://www.facebook.com/majeti.subrahmanyaaditya) HI, JOSEPH NDUNGU i have a problem on calling " /conversations"


controller in user.js its is routs to index of conversations controller
which is not de ned. but i changed to /conversations/create and i just
create conversations/create.html.erb but it is showing 404 i dont know
why popup of message box is not opened
Reply | View 3 Replies

BEN FORREST
11 Apr 15

(https://www.facebook.com/benjyforrest) So useful! Nice one I wondered if anyone managed to get of ine caching of messages
working eg gmail chat when the recipient is of ine it message is stored until they
login
Reply | View 0 Replies

DAVID CHRIQUI
12 Apr 15

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

22/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://www.facebook.com/davchriqui) 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 up
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 KHURSHID


14 Apr 15

(https://github.com/Mubashwer) 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

(https://github.com/bbetter) do we need UserController, i followed this guide but now am getting error uninitialized constant
UsersController
Reply | View 1 Reply

PVIJAYROR
09 May 15

(https://github.com/pvijayror) Thank you working ne... but not 100% useful. Only problem: "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 0 Replies

M-SAKTHIVEL
21 May 15

(https://github.com/m-sakthivel) 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

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

23/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/Anyatskar)

ANNA

21 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, but
it has the same issue as described above. Clues?
Reply | View 5 Replies

ANNA

21 May 15
(https://github.com/Anyatskar) 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

SARAHGUPTA022
10 Jul 15

(https://github.com/sarahgupta022) Hi Joseph, Thank you very much for your very useful blog tutorials.I am trying to put "gmail
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 missing
Missing 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-rails3.2.0/app/views" * "c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/devise3.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 GUPTA
11 Jul 15

(https://github.com/gakshay) Audio was also not getting automatically played. So download any noti cation sound on internet
and put it in public directory naming notify.mp3 and it will start working

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

24/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

Reply | View 0 Replies

PVIJAYROR
18 Jul 15

(https://github.com/pvijayror) Hi Akshay Gupta, Please let me know can you describe to me automatically opening the chat
window
Reply | View 1 Reply

SANJEEV KUMAR MISHRA


05 Jun 15

(https://github.com/sanjusoftware) 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 of ine and based on that
showing some visual on the chat window / hide the send message button etc
Reply | View 3 Replies

PVIJAYROR
10 Jun 15

(https://github.com/pvijayror) Hi Joseph, Thank you for update :) Plase x ASAP automatically opening the chat window
Reply | View 1 Reply

ANIL SINGH
23 Jun 15

(https://github.com/anilsingh826) Hi Joseph, Thanks for this nice tutorial, I am facing one issue with it. Started GET "/faye.js" for
127.0.0.1 at 2015-06-23 15:27:12 +0530 ActionController::RoutingError (No route matches
[GET] "/faye.js"): Could you please help to x this issue. Thanks in Advance :)
Reply | View 0 Replies

ANIL SINGH
23 Jun 15

(https://github.com/anilsingh826) I have xed this issue.

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

25/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

Reply | View 0 Replies

AKSHAY GUPTA
11 Jul 15

(https://github.com/gakshay) I have posted my solution on above thread...for opening the chat box automatically for another
online user
Reply | View 1 Reply

MOUSSASARR
20 Aug 15

(https://github.com/moussasarr) 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 from
here to an inbox system in coordination with the chat messages ? If so, how ?
Reply | View 1 Reply

WEIJIE GAO
27 Aug 15

(https://github.com/balla121) hey! I wrote a short blog post on how to set up the private pub messenger for https/ssl. There was
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.SIVARAMAKRISHNA
14 Sep 15

(https://github.com/siva23) 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 GUPTA
14 Sep 15

(https://github.com/gakshay) Thanks Teki.. good it was helpful...


Reply | View 0 Replies

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

26/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

BRUNO SIQUEIRA
27 Sep 15

(https://github.com/brunosiqueira) Hey, Joseph! Thank you for the great tutorial! private_pub had been outdated for more than
3 years. Do you know if there another gem option? Thanks?
Reply | View 0 Replies

XBITS

23 Sep 15
(https://github.com/xbits) 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 MAGYAR
05 Oct 15

(https://github.com/RealSilo) Hey! Awesome tut! I am pretty new to rails. Could sby tell me how I could embed the chat function
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

BANGOC123
24 Oct 15

(https://github.com/bangoc123) How can i display this chat at any page in my app? Please!!!
Reply | View 2 Replies

LMAHENDRA
05 Oct 15

(https://github.com/lmahendra) 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

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

27/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

LMAHENDRA
05 Oct 15

(https://github.com/lmahendra) 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

MAXMEIRA
29 Oct 15

(https://github.com/maxmeira) Excellent tutorial dear Joseph! Need to put the button "Send Message" on views users / pro le
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

ZELLIMBO
29 Oct 15

(https://github.com/zelLimbo) 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

LEONGG
31 Oct 15

(https://github.com/leongg) Hi Joseph, I got an error: Can't verify CSRF token authenticity


ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken: [...] Have
you seen that error? do you know how can I x it? Many thanks!
Reply | View 2 Replies

RMATEJACABELLO
28 Nov 15

(https://github.com/rmatejacabello) 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

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

28/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

"noti cations" channel. This is also very helpful to know when the user is online or of ine
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 noti cation channel
which would be the same as knowing when the user comes online or of ine. 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

JAN

01 Dec 15
(https://github.com/Edward-Teach) Hello Joseph, I don't know if there was an update but at the moment I receive a 500 internal
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 CARIDI
16 Dec 15

(https://github.com/tomcaridi) 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 code
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

LMAHENDRA
18 Dec 15

(https://github.com/lmahendra) can you provide more details as in what's the error you are getting and RoR version?
Reply | View 4 Replies

PRAGATISIGMA
28 Dec 15

(https://github.com/pragatisigma) 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, I
want to popup remain open for all the pages even i refresh the page like facebook. Any help?
Reply | View 0 Replies

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

29/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

SIREESH
06 Jan 16

(https://github.com/soreddysirish) 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 got
any message.
Reply | View 0 Replies

STEPHANE CEDRONI
07 Jan 16

(https://github.com/stefanocdn) 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 ef ciently fetch the interlocutors with a JOIN
(otherwise this is doing N+1 query). Anybody found a solution for that?
Reply | View 0 Replies

SEBASTIAN VELANDIA
11 Jan 16

(https://github.com/ssoulless) Great tutorial, now how to add le attachments to the messages?


Reply | View 0 Replies

SEBASTIAN VELANDIA
11 Jan 16

(https://github.com/ssoulless) By the way, the demo app is not working!!


Reply | View 1 Reply

KHAVQ
13 Jan 16

(https://github.com/khavq) This is really nice tutorial, I was making it work on local (computer A) but when I using another
computer (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

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

30/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/keyeh)

KEVIN YEH
04 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

HARSHAD26
08 Apr 16

(https://github.com/harshad26) 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

TYLERTAYLOR
15 Feb 16

(https://github.com/TylerTaylor) 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

JOHN OWUOR
23 Feb 16

(https://github.com/owuor91) 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/pro les/5/conversations/1 instead of
localhost:3000/conversations/1 . Any Idea how to go about this?
Reply | View 0 Replies

JOHN OWUOR
25 Feb 16
(https://github.com/owuor91)

gured it out. will make a pull request soon


Reply | View 0 Replies

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

31/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

(https://github.com/harshad26)

HARSHAD26
08 Apr 16

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

KINYODAN
23 Apr 16

(https://github.com/kinyodan) 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 signed
in
Reply | View 0 Replies

KINYODAN
23 Apr 16

(https://github.com/kinyodan) oh managed to get it working


Reply | View 0 Replies

DSCLOUD
30 Apr 16

(https://github.com/dsCloud) 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

DSCLOUD
30 Apr 16

(https://github.com/dsCloud) I am developing on Nitrous environment,


Reply | View 0 Replies

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

32/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

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
Rails

NEW

05 Apr 15
Introduction It's been quite a while since my last tutorial and since then I've recieved alot of requests by email to implement a private
messaging system ...

AJAX SORTABLE LISTS RAILS 4


Ajax

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 RAILS


Rails

20 Oct 14
Often when developing Rails applications, you will nd a need to setup a couple of environment variables to store secure information
such as passwords, a...

GMAIL LIKE CHAT APPLICATION IN RUBY ON RAILS


Chat

30 Jul 14
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://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails

33/34

5/16/2016

Gmail Like Chat Application in Ruby on Rails | Joseph Ndungu

FAST AUTOCOMPLETE SEARCH TERMS - RAILS


Search

13 Jul 14
Introduction In many cases you nd that you need to introduce a global search in your rails application i.e. search multiple models at a
go using one form...

Load more (/tutorials/more)

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

34/34

You might also like