All threads / Group Chat with ActionCable: Part 7 Discussion
Ask A Question


You’re not receiving notifications from this thread.

Group Chat with ActionCable: Part 7 Discussion

Hi! response for the new user

undefined method `last_read_at' for nil:NilClass for Chatrooms#show

how to solve?

Hey Nikola, looks like you don't have a @chatroom_user record set. That was one of the things we discovered was missing, so you'll want to make sure that your user's got that record when they join. We made a couple tweaks to this to fix a couple bugs on Github, so you might want to check those out:

Hey Chris,

I couldn't find good resources on how to test actioncable with rspec + capybara. Don't you know some great articles/repos I could check out?

Unfortunately a lot of it is still up in the air directly from Rails. Hoping to see this start being more available soon.

Thanks Chris for the quick answer. I know that it's not released yet, but I thought something could be done with acceptance test like this: But for some reason (likely actioncable related) I can't make it work. Do you have any idea?

If I get some time this weekend I'll give it a try and see. I would expect that to work, but there might be some gotchas.

Does anyone have an update on the rspec / actioncable issue? We are slowly starting to implement rspec into our workflow as our app gets bigger.


I implemented the unread messages feature, however, when messages are sent in the chatroom and a user has not joined the room prior to that, it sends this error once the join button is clicked.

By joining the chatroom, I mean clicking the join button, which then adds the room to the list of joined rooms of the user before he enters the room.

This is in the rooms/views/show.html.erb https://uploads.disquscdn.c...
Thank you!

Check out the code on Github, we fixed that bug in the repository. 👍

Thank you. Is it fixed through the json.jbuilder files for both the show and the index?

It's missing setting the last_read_at when you join a new channel. Should be in the chatroom_users controller.

oh thank you! I just figured it is the same issue that was discussed in this same discussion.

Hi Chris,

When showing the list of chatrooms (chatrooms#index), I would like to display the usernames of the users in that room along with the name of the room.

Do you think this would be a good use case for a array/jsonb column type in the chatrooms, in order to store information about the users subscribed to that chatroom?

That would avoid extra table joins on the users table, but would have the downside of needing to update the chatrooms when a user updates his username.

The alternative would be to go with a regular table join on the users table when fetching the chatrooms.
Thanks for your feedback on this.

Hey Stephane, I would do a join table for this because as things get more complex you'll want to probably support extra features like different roles for users in the room (like Slack allows you to invite guests to specific rooms) and the best way to do that would be with the join table. They're still fast to query and can store all the additional data you might want on it.

Hi Chris, thank you for your response. That makes more sense to go with a Join, I agree with you, thanks for taking the time to answer!

Hi Stephene,

If I understand, your chat room displays the names of the users who are currently in that chatroom chatting? Did you figure out how to get it working?


Nguyen Huy Anh ·

Hi chris,how we cat set passwork for chat room to private chat

Hi Chris,

I just finished episode 7 of Group Chat with ActionCable. When I send a message, the "unread messages" div appears in the browser window of the sender as well as the recipient's window. Any ideas why it's appearing in the message sender's window?

<% unread_messages = false %>
<div data-behavior="messages" data-barroom-id="&lt;%= %&gt;">
<% @messages.each do |message| %>
<% if !unread_messages && @barroom_user.last_read_at < message.created_at %>
<% unread_messages = true %>
<div class="strike">
Unread Messages
<% end %>
<%= render message %>
<% end %>

def show
@messages = @barroom.messages.order(created_at: :desc).limit(100).reverse
@barroom_user = current_user.barroom_users.find_by(barroom_id:

App.barrooms = App.cable.subscriptions.create "BarroomsChannel",
connected: ->
# Called when the subscription is ready for use on the server

disconnected: ->
# Called when the subscription has been terminated by the server

received: (data) ->
active_barroom = $("[data-behavior='messages'][data-barroom-id='#{data.barroom_id}']")
if active_barroom.length > 0

if document.hidden
if $(".strike").length == 0
active_barroom.append("<div class="strike">Unread Meassages</div>")

if Notification.permission == "granted"
new Notification(data.username, {body: data.body})


active_barroom.append("<div>#{data.username}: #{data.body}</div>")

$("[data-behavior='barroom-link'][data-barroom-id='#{data.barroom_id}']").css("font-weight", "bold")

send_message: (barroom_id, message) ->
@perform "send_message", {barroom_id: barroom_id, body: message}


Are you still seeking an answer to this? If so, let me know, and I'll see if we can help you. We just got our chatroom working properly.


Great series. The best on ActionCable I've seen so far and I've been trying to learn it for a couple of weeks.

Jillis Stada ·


I have appended a chatroom to the chatroom list.
on recieved: (data) I managed to subscibe to all channels again in with

received: (data) ->
chatroom_list = $("[data-behavior='chatrooms']")
# Insert the chateroom
chatroom_list.append("< li>< a data-behavior='chatroom-link' data-chatroom-id='#{data.chatroom_id}' href='\/chartrooms\/#{data.chatroom_id}'>< strong<#{data.chatroom_name}< /a>< /li>")
App.cable.subscriptions.create "ChatroomsChannel"

How do I subscribe only to the the recieved chatroom?

Jillis Stada ·

App.cable.subscriptions.create {channel:"ChatroomsChannel", room: "#{data_chatroom_id}"}

Excelent Series!!... everything works perfect!

Hi Chris!

We got it working perfectly! Thanks so much! Can't wait to show you what we're putting together in a few months when we launch :D

One question: when a chatroom switches to bold, it indicates new unread messages. When I click on that chatroom, it loads the chatroom, but then it takes about 3 - 5 seconds before the new messages themselves load.

Do you have any suggestions on how to remove that load delay?


Learned a ton on this one! Thanks

This serie is a masterpiece - learned much. As i am using Rails 6 i was struggling several times - and gave up for a while. There are big changes between Rails 5 and 6 and also ActionCable was getting improved. Some updates here after almost 4 years since creation would be very very helpful for all the Members who are starting a new Apps - they will do it for sure with Rails 6. Thanks in advance for any helps.

Update to this series is in the works. :)

thanks for this super fast reply. by far the best news today. stay safe.

I've tried to use if else statement, and display a delete link next to the message, when I submit a message, the delete link won't display, and I have to refresh the page. It seems like if else statement doesn't work in actioncable.

Hey Chris just to let you know, document.hidden did not work for me, so i test documen.hasFocused() == true and it works just fine.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 68,326+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.