Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Action Cable] Support for subscription from other websocket client. #28447

Closed
zw963 opened this issue Mar 16, 2017 · 7 comments
Closed

[Action Cable] Support for subscription from other websocket client. #28447

zw963 opened this issue Mar 16, 2017 · 7 comments
Labels

Comments

@zw963
Copy link

@zw963 zw963 commented Mar 16, 2017

I want to subscription a channel from a android client.

I think I had connect to the server with websocket android client library.

I can see response message like following:

HTTP/1.1 101 Web Socket Protocol Handshake
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 16 Mar 2017 13:22:18 GMT
Connection: upgrade
Upgrade: WebSocket
WebSocket-Origin: action-cable.ershouweb.test.xthinkapp.com
WebSocket-Location: ws://action-cable.ershouweb.test.xthinkapp.com/cable

{"type":"welcome"}�{"type":"ping","message":1489670541}�{"type":"ping","message":1489670544}�{"type":"ping","message":1489670547}�{"type":"ping","message":1489670550}

I research action-cable coffee source code, and want to subscription to a channel.
Following is message string I sent to server:

{"command": "subscribe","identifier": {"channel":"App::UserChatChannel","current_chatroom_id":"12121212","subscribed_user_id":"1212121"}}

I got following error:

E, [2017-03-16T21:30:23.880658 #27250] ERROR -- : Could not execute command from {"command"=>"subscribe", "identifier"=>{"channel"=>"App::UserChatChannel", "current_chatroom_id"=>"12121212", "subscribed_user_id"=>"1212121"}}) [TypeError - no implicit conversion of Hash into String]: /data_1/www/ershou_web/ershou_web_staging/shared/bundle/ruby/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize' | /data_1/www/ershou_web/ershou_web_staging/shared/bundle/ruby/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `new' | /data_1/www/ershou_web/ershou_web_staging/shared/bundle/ruby/2.2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse' | /data_1/www/ershou_web/ershou_web_staging/shared/bundle/ruby/2.2.0/gems/activesupport-5.0.2/lib/active_support/json/decoding.rb:21:in `decode' | /data_1/www/ershou_web/ershou_web_staging/shared/bundle/ruby/2.2.0/gems/actioncable-5.0.2/lib/action_cable/connection/subscriptions.rb:27:in `add'

I am not found any document for this purpose, is there exist some issue about this?

Thanks.

@zw963

This comment has been minimized.

Copy link
Author

@zw963 zw963 commented Mar 17, 2017

Hi, It worked now, it should some form like this:

{"command":"subscribe","identifier":"{\"channel\":\"App::UserChatChannel\",\"current_chatroom_id\":\"1470155475503808512\"}"}

nest double quotes need escape. so, I think this should not a issue.

Sorry.

@zw963 zw963 closed this Mar 17, 2017
@adancondori

This comment has been minimized.

Copy link

@adancondori adancondori commented Jun 17, 2017

zw663 could you share any example about connettion android and actioncable.

@zw963

This comment has been minimized.

Copy link
Author

@zw963 zw963 commented Jun 17, 2017

@adancondori , as I say in previous reply, you need send a special json to success subscribe
action_cable channel.

following is those example:

{"command":"subscribe","identifier":"{\"channel\":\"App::UserChatChannel\",\"current_chatroom_id\":\"1470155475503808512\"}"}

Let say with ruby terminology, you need run {channel: "....", any_data: "..."}.to_json get a string,
and, suppose this string is saved as a variable data, you need run {command: "subscribe", idetifier: data}.to_json again to get result and sent to action-cable server, it will register success.

@zw963

This comment has been minimized.

Copy link
Author

@zw963 zw963 commented Jun 17, 2017

@spidergears

This comment has been minimized.

Copy link

@spidergears spidergears commented Mar 17, 2018

Why does this have to be so cryptic, why can't it work with a plain JSON object.

@zedtux

This comment has been minimized.

Copy link

@zedtux zedtux commented Aug 28, 2018

Thank you very much @zw963 for your very useful comments ! 👍

Hopefully the following could help people like @spidergears. Here is how I'm building the message to be sent to Rails :

      const subscribeCommand = JSON.stringify({
        command: 'subscribe',
        identifier: JSON.stringify({
          channel: 'ChatChannel',
          user_id: 1
        })
      })

And this works fine.

@nathan-eko

This comment has been minimized.

Copy link

@nathan-eko nathan-eko commented Feb 7, 2019

Why does this have to be so cryptic, why can't it work with a plain JSON object.

They use that identifier string as a Hash key to look up subscriptions. I don't think this is a very good reason for making it so impenetrably silly, but it's a reason of some kind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.