Skip to main content

Group Chat with ActionCable: Part 7 Discussion

General • Asked by Chris Oliver

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: https://github.com/gorails-...


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. https://github.com/rails/ra... 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: http://bit.ly/2dfDTnC. 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.


Hi,

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?

-Monroe


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?

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

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

channels/barrooms.coffee
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})

else
App.last_read.update(data.barroom_id)

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

else
$("[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}

Rex,

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.

-Monroe


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


Hi,

I have appended a chatroom to the chatroom list.
on recieved: (data) I managed to subscibe to all channels again in Users.coffee 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?

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?

Thanks!
-Monroe


Learned a ton on this one! Thanks


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.