0

Basically I am trying to add live chatrooms to my Rails website. In my app there are 2 models: Client and Professionnel. Clients can ask quotes to professionels. (Edit: Quote is a model too)

For each quote I want to have a live chat available between client and professionnel.

I have already done it with a classic Message channel, appending the right message to the right quote. Yet it doesn't fulfill privacy principle as every Client and every Professionnel receive all chat messages.

At server level, I can easily subscrible the user (Client or Professionnel) to a quote specific stream like :

class QuoteChannel < ApplicationCable::Channel
  def subscribed

    if current_user 

      if current_user.quotes.any?
            current_user.quotes.each do |quote|
                stream_from "#{quote.hashed_id.to_s}_channel"
            end
      end
    end

  end

  def unsubscribed       
  end
end

Though I am a bit stuck for the client side. My quote.coffee.erb doesn't accept that I use current_user (as defined in Actioncable connection file) or any Devise identifier current_client or current_professionnel.

So I am not sure how I can personnalize my subscriptions on the client side. I have read that it is possible to catch an element broadcast in the message but I am not sure how I can do with my current Coffee.erb file :

App.quote = App.cable.subscriptions.create "QuoteChannel",
  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) ->
    $('#cell'+data.quoteid).append '<div>'+'<span>'+data.message+'</span>'+'</div>'
    $('.sendmessageinputtext').val("")

quoteid is passed to the received function but I need to create as many streams as the user owns quotes. In this thread http://www.thegreatcodeadventure.com/rails-5-action-cable-with-multiple-chatroom-subscriptions/ the tutor iterates across all available Chatrooms, I could do the same but it is stupid as there may be thousands of live quotes at a certain time, of which only a few are owned by the current_user which has been allowed an Actioncable connection.

Maxence
  • 2,029
  • 4
  • 18
  • 37
  • Since each user subscribes to quotes that are associated with them what is the privacy issue ? Since during subscription user subscribes to all quotes so I dont see a problem there I dont understand what are your requirements. – GorillaApe Apr 20 '18 at 07:11
  • You're correct. Server side the right users are subscribed to the right quotes flow, but if client receive every flow, I guess this is not different than having a single stream. But to be honest I am still struggling with the logic of Actioncable.. You may actually be correct. What I would like to do is grab the page url param and, because the JS being ERB, subscribe client side to the right quote flows. But I can't use the `current_`helper neither the params (not sure why). I guess I have to dive deeper. I am still struggling understanding is a framework alongside Rails and doesnt really mix. – Maxence Apr 20 '18 at 09:54
  • 1
    I can't understand what you want to do however if you want to subscribe for a specific quote you can pass it as parameter during subscribe. Or you could add a method at channel and call stream_from from there. However you should understand that actioncable is just for pub/sub with limited features. So you still need some kind of api to get older messages / active quotes etc. – GorillaApe Apr 20 '18 at 10:34
  • it's for having a chat channel for each quote. One client can see all quotes asked (and updated by professional with price etc ..) and also have a chat box to communicate with the professional. So basically there are multiple chats on a single page (provided client has requested different quotes from different professionals). To my understanding the best would be to grab the current client and, in the coffee, load all quotes that this very client has requested, and generate as many subscriptions as needed, just like I did server side on the subscription file quoted above. – Maxence Apr 20 '18 at 11:03
  • Oddly if I try to grab the URL for the client in this "quote" view, the method is unknown. I think my problem is explained here : https://stackoverflow.com/questions/15053678/rails-path-helpers-doesnt-work-in-js-coffee-erb My coffee ERB is not in context then cannot really interact with the rest of the page. – Maxence Apr 20 '18 at 11:10
  • 1
    As far as I know although I haven;t tested it you can have wildcard subscriptions at least redis supports them. For example client_999_quote_* . ERB code in javascript runs only once so you cannt have the behavior you want. You can export some js data at page or call api. For exaple /client/quotes that would return a json with quotes and last 10 messages. Then you would subscribe to actioncable. When receiving data you could append. There are several cases to handle though such as disconnection/ new quotes etc – GorillaApe Apr 20 '18 at 12:11
  • Thank you, I will check the wildcard subscriptions suggestion. Also whiat I was thinking coule be possible is add a javascript script for each quote on the "quotes" view. This time it should be evaluated properly unlike the coffescript file. This is maybe what you say "export some js data at page". I am not very good with JS and this is probably why my options are a bit limited at the moment but I will work on it. Thank you – Maxence Apr 21 '18 at 22:55
  • too late but you can always call stream_from multiple times though – GorillaApe Jun 09 '18 at 21:33

0 Answers0