0

I am having a hard time passing information properly from a python script to a rails app through web sockets (using Action Cable).

I want to use input information in a python dictionary like this :

message = {
    'command':"subscribe",
    'identifier':{
            'channel': 'RaspChannel',
            'rasp_local_id': '1'
        }
    }

It should then be converted into JSON, go through a web socket set by Action Cable, then parsed into a ruby hash on the server side.

For some reason, I only made it work by sending a plain string through the socket :

'{"command":"subscribe","identifier":"{\\"channel\\":\\"RaspChannel\\", \\"rasp_local_id\\": \\"1\\"}"}'

But I could not make it work by sending json.dumps(message) even though it returned something that looked pretty good to me :

{"command": "subscribe", "identifier": {"channel": "RaspChannel", "rasp_local_id": "1"}}

I suppose action cable protocol uses the classic ruby JSON.parse method on the json string it receives as input, and for some reason, it always throws a parsing error, unless I use the plain string I mentioned above.

However, as I want to make the message more complicated, it would be better if I could make it work with actual Python dictionaries from the beginning.

Any idea on what I am missing ?

Ouhbelle
  • 33
  • 4
  • read about methods as_json and to_json for string in ruby. It might help – ARK Feb 03 '20 at 20:41
  • I looked into it, but actually, I can't do much on the ruby side of things, because action cable server throws errors coming from the JSON encoded in Python – Ouhbelle Feb 03 '20 at 22:23
  • I have very limited knowledge of python but sending as json might help? https://stackoverflow.com/questions/26745519/converting-dictionary-to-json – ARK Feb 04 '20 at 07:49

1 Answers1

0

After more research, it seems that the issue did not come from either JSON python encoding, ruby decoding, or me misusing any of these methods.

Actually, it would seem that the protocol used by actioncable when receiving messages does not exactly rely on a "classic" JSON, but uses "escaped JSON within the JSON" for the identifier

More on that on the rails github issue here https://github.com/rails/rails/issues/22675

Ouhbelle
  • 33
  • 4