Ask A Question

Notifications

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?

Reply

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-...

Reply

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?

Reply

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.

Reply

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.

Reply

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!

Reply

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

Reply

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.

Reply

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.

Reply

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

Reply
Nguyen Huy Anh Nguyen Huy Anh

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

Reply

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}

Reply

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

Reply

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

Reply
Jillis Stada Jillis Stada

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?

Reply

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

Reply

Excelent Series!!... everything works perfect!

Reply

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

Reply

Learned a ton on this one! Thanks

Reply

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.

Reply

Update to this series is in the works. :)

Reply

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

Reply

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.

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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

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

    © 2024 GoRails, LLC. All rights reserved.