Search code examples
ruby-on-railsruby-on-rails-5actioncable

Rails Action Cable: How to fetch User Agent & Client IP Address?


I am upgrading a Rails app to use websocket via Action Cable. For logging purposes, I need to get the client's IP address and the user agent who sends the socket message.

In the speak method of MyChannel class (app/channels/my_channel.rb), I cannot access the session or the request.

Do you have any idea on how I can get user_agent and the client IP address in this speak method?


Solution

  • You may get this info from request in Connection object to which Channel belongs.