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

[Question] JSON RPC API #109

Closed
jared-94 opened this issue Sep 20, 2016 · 21 comments
Closed

[Question] JSON RPC API #109

jared-94 opened this issue Sep 20, 2016 · 21 comments

Comments

@jared-94
Copy link

Hi,

I don't really understand how to remotely get, or set the status or volume of a client thanks to JSON RPC API.
The port seems to be 1705.

May I achieve this with a HTTP request?
My aim is to integrate Snapcast to the Fibaro Home Center 2 (a smart box)

Sorry for my stupid question...

@happyleavesaoc
Copy link
Contributor

Open a TCP connection on port 1705. Send and receive messages formatted according to JSON RPC spec. You can see an implemention in Python here.

@jared-94
Copy link
Author

Thank you for your answer.
It works fine with the Python script!

But I can't install the Python package on my box.
Is it possible to control Snapcast with a simple URL request?
If so, could you please provide an example?

@happyleavesaoc
Copy link
Contributor

You should be able to use curl with the --data-binary argument and a valid JSONRPC message.

@jared-94
Copy link
Author

I've tried without success with the Chrome extension POSTman:
snap

@1337sup3rh4x0r
Copy link

Your Message Looks good but I don't have a clue whether it can work via post. But from what happyleavesaoc said, I would try binary instead of raw

@jared-94
Copy link
Author

OK finally it works with JSON api, I've added "\r\n" at the end of my request.

Other question : Is there a JSON method for connecting or disconnecting a specified client?

@badaix
Copy link
Owner

badaix commented Sep 28, 2016

Yep, the snapserver expects "Newline Delimited JSON" (ndjson). Not wrapped into HTTP, but pure ascii json over TCP. The newline \n is used to mark the end of a json message.

There is a "spec" (work in progress) here:
https://github.com/badaix/snapcast/tree/master/doc/json_rpc_api

You can find a java implementation in the Android client's source code.

There is no method to connect or disconnect a client. You can mute/unmute a client, but you cannot disconnect a client.
What is the idea behind connecting/disconnecting a client?

BTW: thanks for the hint about POSTman 👍

@jared-94
Copy link
Author

This is what I do, I mute the client where I don't want to listen music, but I suppose the stream is still sent to the client.
In order to save bandwidth on my LAN, I think it whould be better to disconnect the client (like Logitech Media Server do)

@badaix
Copy link
Owner

badaix commented Sep 29, 2016

true, but the "control connection" must be there to be able to reconnect.
Good idea, but not yet implemented.

@badaix
Copy link
Owner

badaix commented Mar 14, 2017

closing, as it is related to #150

@badaix badaix closed this as completed Mar 14, 2017
@freedreamer82
Copy link

hi, same issue here: should be possible to post an example of the json message to use ? i tried several time with no luck... i would like to use a curl command.

@badaix
Copy link
Owner

badaix commented Sep 29, 2017

There are examples here.
Curl will use HTTP, but you have to use raw TCP (i.e. without HTTP header, just the json messages, delimited by '\n')

@freedreamer82
Copy link

thanks for quick reply: something like that should be ok ? 'cause doesn't work

curl --data-binary '{"id":8,"jsonrpc":"2.0","method":"Server.GetRPCVersion"}\n' 127.0.0.1:1705

@badaix
Copy link
Owner

badaix commented Sep 29, 2017

curl will still use HTTP, when I get it right:
from curl -h:
--data-binary DATA HTTP POST binary data (H)

@freedreamer82
Copy link

Hi, If u are interested I developed a python deamon that listens and bridges http request to raw socket.I can share it pushing on github if u want. regards

@badaix
Copy link
Owner

badaix commented Oct 4, 2017

Yes, why not 😃
People often ask for some HTTP-like access. It would be quite easy to natively support json wrapped in HTTP POST or GET as done in Kodi.
The main problem (also true for REST) is the lack of push notifications. Kodi supports notifications via raw TCP (as Snapcast does) and via WebSockets (I don't know how convenient WebSockets are for clients).
On the other hand, I was thinking of an external bridge, which would keep Snapcast small and still generic. Hence your approach sounds highly interesting.

I will reopen this issue. Just to collect some more ideas, as my knowledge of what kind of "webservice" is most convenient is rather limited.

@badaix badaix reopened this Oct 4, 2017
@freedreamer82
Copy link

Perfect , I'll clean the code and I'll push on github. I you like it i'll do a pull request.
I tried with garmin smartwatch simulator to control music with the watch and it works.

I'll write you an email when ready.
Marco

@freedreamer82
Copy link

https://github.com/freedreamer82/snapcastHttpd

try it with :
curl -d '{"id":8,"jsonrpc":"2.0","method":"Server.GetRPCVersion"}' 127.0.0.1:7777

let me know if it works from your side.

Marco

@mgafner
Copy link

mgafner commented Nov 27, 2017

curl didn't work for me.

i managed to get an answer from the snapcast server with that command:
echo '{"id":8,"jsonrpc":"2.0","method":"Server.GetRPCVersion"} ' | nc -w 1 <ip-of-snapcast-server> 1705

the space after GetRPCVersion"} and ' is very important. without it I got an error. it took me long time to find this hack...

@jaedb
Copy link

jaedb commented Mar 25, 2018

@badaix to answer your question of how convenient Websockets are for clients, the answer (in my opinion) is: very!

Connecting via TCP is generally limited to server-side clients, whereas Websockets are client-ready as well a server-ready. In my experience Websockets are much more widely supported than TCP. This is supported by the number of people asking for help connecting to the Snapcast API from their applications.

I believe the request for Websocket functionality is already opened: #250. Do you think this is something that you'll be looking to implement?

@badaix
Copy link
Owner

badaix commented Sep 15, 2020

HTTP Put and Websockets are meanwhile implemented and accessible via port 1780

@badaix badaix closed this as completed Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants