0

I'm making chat using ActionCable. I'd like to have normal, public rooms, private rooms and direct messages (something like Slack).

Public rooms and DMs are working. I can also create private room and add users. But I'd like to have some way to notify user, that he was added to private room. Now, user needs to refresh page for this new room he was added to appear in his chats list.

I started slowly to catch this moment when user is added by other user,I wanted to have something written to console and have link to this new room but it doesn't work. "Chatroom isn't active" shows when someone writes something in room user belongs to, but in this specific situation - being added to freshly created room - it doesn't show at all, same thing with the link.

App.chatrooms = App.cable.subscriptions.create "ChatroomsChannel",
  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_chatroom = $("[data-behavior='messages'][data-chatroom-id='#{data.chatroom.id}']")
    if active_chatroom.length > 0
      console.log 'chatroom is active"
      active_chatroom.append("<div class='message'>
      <a href='' class='message_profile-pic'></a>
      <a href='' class='message_username'><strong>#{data.email}</strong></a>
      <span class='message_star'></span>
      <span class='message_content'>
      #{data.body}
      </span>
      </div>")
      debugger
    else

      console.log 'chatroom isnt active'

      $("[data-behavior='chatroom-link'][data-chatroom-id='#{data.chatroom.id}']").css("font-weight", "bold")
      chatroom_id = data.chatroom.id
      $('tbody').append("<tr><td><a href='/chatrooms/#{data.chatroom.id}'>Name</a></td></tr>")
    $messages = $('#messages')
    $messages.scrollTop $messages.prop('scrollHeight')

  speak: (chatroom_id, message) ->
    @perform 'speak', {chatroom_id: chatroom_id, body: message}
Jes
  • 323
  • 2
  • 18

0 Answers0