[Feature][ActionCable] Possibility from server side to terminate subscription by specifying WebSocket close code and reason

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActionCable] Possibility from server side to terminate subscription by specifying WebSocket close code and reason

Artis Abolts

WebSocket.close() method accept two optional parameters:

  1. code - A numeric value indicating the status code explaining why the connection is being closed.
  2. reason - A human-readable string explaining why the connection is closing.

More info: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close

 


Real live example where and how my proposed changes would work in Rails

 

For example by terminating subscription from server if User session expired

 

        ActionCable.server
                   
.remote_connections
                   
.where(current_user: warden_proxy.user)
                   
.disconnect(4001, "Unauthorized")


On JavaScript side possibility to receive WebSocket close code and reason. For example if WebSocket closed because User session expired then we redirect to root path

 

      disconnected: (data) ->
       
# Called when the subscription has been terminated by the server

       
# If Unauthorized (Session Terminated or Expired)
       
if data.statusCode == 4001
          window
.location = "/"  



In Rails ActionCable there is needed only couple changes to support WebSocket close code and reason.

Please check my implementation in my Fork: https://github.com/aboltart/rails/commit/be168f16118f5848d0d3a9165afeafb6717a71f8

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActionCable] Possibility from server side to terminate subscription by specifying WebSocket close code and reason

T.J. Schuck
See also https://github.com/rails/rails/pull/29390 about using proper close codes for WebSockets.

On Thu, Oct 4, 2018 at 1:45 PM, Artis Abolts <[hidden email]> wrote:

WebSocket.close() method accept two optional parameters:

  1. code - A numeric value indicating the status code explaining why the connection is being closed.
  2. reason - A human-readable string explaining why the connection is closing.

More info: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close

 


Real live example where and how my proposed changes would work in Rails

 

For example by terminating subscription from server if User session expired

 

        ActionCable.server
                   
.remote_connections
                   
.where(current_user: warden_proxy.user)
                   
.disconnect(4001, "Unauthorized")


On JavaScript side possibility to receive WebSocket close code and reason. For example if WebSocket closed because User session expired then we redirect to root path

 

      disconnected: (data) ->
       
# Called when the subscription has been terminated by the server

       
# If Unauthorized (Session Terminated or Expired)
       
if data.statusCode == 4001
          window
.location = "/"  



In Rails ActionCable there is needed only couple changes to support WebSocket close code and reason.

Please check my implementation in my Fork: https://github.com/aboltart/rails/commit/be168f16118f5848d0d3a9165afeafb6717a71f8

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActionCable] Possibility from server side to terminate subscription by specifying WebSocket close code and reason

Artis Abolts
Yes, I saw your mentioned Merge Request. 

In my example I am using 4000+ WebSocket close code what according https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes description should be used for application specific purpose.

In my implementation I am not performing status code checking, because at then end status code validation will be performed https://github.com/rails/rails/blob/5-2-1/actioncable/lib/action_cable/connection/client_socket.rb#L90


On Friday, October 5, 2018 at 9:56:20 PM UTC+3, T.J. Schuck wrote:
See also <a href="https://github.com/rails/rails/pull/29390" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F29390\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnsUlUleOVyjGRyEcEb8SPHtfoBg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F29390\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnsUlUleOVyjGRyEcEb8SPHtfoBg&#39;;return true;">https://github.com/rails/rails/pull/29390 about using proper close codes for WebSockets.

On Thu, Oct 4, 2018 at 1:45 PM, Artis Abolts <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ubL0SEaNAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">artis....@...> wrote:

WebSocket.close() method accept two optional parameters:

  1. code - A numeric value indicating the status code explaining why the connection is being closed.
  2. reason - A human-readable string explaining why the connection is closing.

More info: <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FWebSocket%2Fclose\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGrLSve_fLfm6RYMnV4f8FRUjaC1Q&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FWebSocket%2Fclose\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGrLSve_fLfm6RYMnV4f8FRUjaC1Q&#39;;return true;">https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close

 


Real live example where and how my proposed changes would work in Rails

 

For example by terminating subscription from server if User session expired

 

        ActionCable.server
                   
.remote_connections
                   
.where(current_user: warden_proxy.user)
                   
.disconnect(4001, "Unauthorized")


On JavaScript side possibility to receive WebSocket close code and reason. For example if WebSocket closed because User session expired then we redirect to root path

 

      disconnected: (data) ->
       
# Called when the subscription has been terminated by the server

       
# If Unauthorized (Session Terminated or Expired)
       
if data.statusCode == 4001
          window
.location = "/"  



In Rails ActionCable there is needed only couple changes to support WebSocket close code and reason.

Please check my implementation in my Fork: <a href="https://github.com/aboltart/rails/commit/be168f16118f5848d0d3a9165afeafb6717a71f8" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Faboltart%2Frails%2Fcommit%2Fbe168f16118f5848d0d3a9165afeafb6717a71f8\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFrLMt9lCsK_rer2aCvcESoxta8qg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Faboltart%2Frails%2Fcommit%2Fbe168f16118f5848d0d3a9165afeafb6717a71f8\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFrLMt9lCsK_rer2aCvcESoxta8qg&#39;;return true;">https://github.com/aboltart/rails/commit/be168f16118f5848d0d3a9165afeafb6717a71f8

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ubL0SEaNAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ubL0SEaNAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.