How to close connection in Action cable?

后端 未结 5 483
时光取名叫无心
时光取名叫无心 2021-01-12 10:59

How to disconnect a client in Action cable (rails 5)? I would like the user to be completely disconnected (similar to when he closes the tab).

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 11:47

    Disconnecting from the client

    I stumbled across this issue too. But I could not believe that there is no simple way to disconnect the websocket connection from the client (without doing an API call). Luckily this works for me:

    // Create consumer
    window.cable = ActionCable.createConsumer(...)
    
    // Subscribe to channels
    window.cable.subscriptions.create('SomeChannel', ...);
    
    // At some point we want to disconnect (e.g. when user logs out)
    window.cable.subscriptions.consumer.disconnect();
    

提交回复
热议问题