Z
Z
Zaur Ashurbekov2017-09-11 01:49:34
Ruby on Rails
Zaur Ashurbekov, 2017-09-11 01:49:34

How to use cable correctly?

Hello Toaster!
In rails 5, a convenient cable for web sockets appeared, but I am tormented by how to properly organize the process of data exchange and processing. Where can you read about it? For example, a case - comments for posts in VK: I uploaded 30 latest news.
1) Now I have to subscribe to changes for all 30 news separately? Well, I have 300 people in my friends, will I subscribe to each message chat?
2) How to create a new comment: through the restful interface, and then broadcast through broadcast? Or through channels by creating/changing/deleting a new comment inside?
3) How to properly organize user access to subscriptions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2017-09-14
@zaurius

1) You need to subscribe by id to a specific user, if a new message (news, etc.) appears, we will not receive it, because you were not subscribed to this dialogue (news).
Subscription Example

def subscribed
  stop_all_streams
  stream_from "conversations:#{current_user.id}"
end

2) It is possible and so and so.
3) User authorization example for devise
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', "User #{current_user.id}"
    end

    protected

    def find_verified_user
      if (verified_user = env['warden'].user)
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question