Answered

How Sonos plays a music from my home speakers

  • 25 February 2021
  • 23 replies
  • 141 views

Badge

Hi,

I have 2 sonos one. Every thing works well but I am a technical question. I hope I posted in a right subsection.

I would like to know how sonos sends and plays a music which I send it from a different network like my mobile data. How it finds my home speaker IP and port?

icon

Best answer by Ralpfocus 3 March 2021, 09:52

When the sonos speaker communicates to the sonos cloud it opens a long running connection with the sonos cloud which will have very low impact as @controlav says. Once a connection is open between two devices they both know an ip address (endpoint) that enables data to flow between them. The endpoint at your end will normally  be the external ip adress of your router - this is assigned by your Internet provider.  When the sonos cloud “responds" on that connection it talks to the endpoint on your router. Your router keeps track of open connections and when a message arrives for your sonos device the router “knows" which device initiated the connection and forwards the message onto that device. 

Sonos therefore knows the ipaddress of your house, your external ip address which can change -  it is under the control of your provider.  Some never change some, less common, change quite regularly. If you use a vpn provider then the endpoint sonos sees is the vpn providers.

By default your home router prevents people from outside initiating connections so the Sonos cloud cannot initiate the connection, your sonos system initiates the connection probably at start up.

@James L. is right that the sonos cloud does not need your local address because it does not initiate the connection but once the connection is open it does know your routers address and despite what he says the message sent to the cloud  probably contains the ip address of the  speaker

As an aside the provider e.g. sonos can use the endpoint ip address to roughly get the physical location of your router. This is one of the ways that companies can target advertising.

Edited to make a correction about knowing local ip address

View original

23 replies

Userlevel 7
Badge +17

As far as I know connecting to Sonos from outside your networkl is only possible through Spotify Connect. If so, this isn't a Sonos question. Or do you mean something else?

The Sonos system runs on a computer inside each speaker. Using the controller as a remote control, you tell that computer on the speakers where to reach out and get that data ftprom. For streaming sources, the software running on each speaker has predefined locations to reach out to a server to get the data from the streaming company. For playing data ‘on this mobile device’, the controller should send its current location to the software running on the speakers so that the speaker can connect to that device to get the data to play.

This is one reason you can only play ‘from this device’ and not ‘from that device’, since the address could change as it leaves and enters the network. And also why Sonos doesn’t allow playlists that contain music ‘on this device’, since a mobile device can leave the network. 

As far as I know connecting to Sonos from outside your networkl is only possible through Spotify Connect. If so, this isn't a Sonos question. Or do you mean something else?

 

Actually, I believe Sonos implemented a cloud server when Alexa integration went live.  So generally, apps/services that can control your Sonos system from outside your home network will access the Sonos cloud server, which then gives the command to your Sonos speaker.  The speaker itself will then execute the command, usually to connect to a streaming service and play audio.  It’s not just Spotify, but Amazon, Pandora, and a few others as well.  And there are some IFTTT stuff you can do as well.

 

That’s not my understanding. The cloud services are, to my knowledge, provided not by Sonos, but by Amazon and Google, for their respective voice systems. Sonos just points to whichever cloud service you’ve set up as a voice control, in exactly the same manner as a streaming music source.

At least, that’s the way I perceive it to work. Feel free to point at anything that suggests I’m incorrect. 

That’s not my understanding. The cloud services are, to my knowledge, provided not by Sonos, but by Amazon and Google, for their respective voice systems. Sonos just points to whichever cloud service you’ve set up as a voice control, in exactly the same manner as a streaming music source.

At least, that’s the way I perceive it to work. Feel free to point at anything that suggests I’m incorrect. 

 

I don’t think Amazon and Google cloud services talk directly to the Sonos speakers. The commands are passed through a Sonos cloud first.  I remember having discussions with Ryan S about this back when Alexa control was released...but that was a long time ago and I could be mis-remembering.

Badge

As far as I know connecting to Sonos from outside your networkl is only possible through Spotify Connect. If so, this isn't a Sonos question. Or do you mean something else?

 

It works for all major music stream providers not only spotify. 

Badge

The Sonos system runs on a computer inside each speaker. Using the controller as a remote control, you tell that computer on the speakers where to reach out and get that data ftprom. For streaming sources, the software running on each speaker has predefined locations to reach out to a server to get the data from the streaming company. For playing data ‘on this mobile device’, the controller should send its current location to the software running on the speakers so that the speaker can connect to that device to get the data to play.

This is one reason you can only play ‘from this device’ and not ‘from that device’, since the address could change as it leaves and enters the network. And also why Sonos doesn’t allow playlists that contain music ‘on this device’, since a mobile device can leave the network. 

I do not get that. how can I play a music from my cellphone with mobile data on my sonos speaker conencted to my home wifi with dynamic IP? my cellphone would have many different IPs and networks. But I can always send musics to my sonos instantly and it plays. 

Badge

As far as I know connecting to Sonos from outside your networkl is only possible through Spotify Connect. If so, this isn't a Sonos question. Or do you mean something else?

 

Actually, I believe Sonos implemented a cloud server when Alexa integration went live.  So generally, apps/services that can control your Sonos system from outside your home network will access the Sonos cloud server, which then gives the command to your Sonos speaker.  The speaker itself will then execute the command, usually to connect to a streaming service and play audio.  It’s not just Spotify, but Amazon, Pandora, and a few others as well.  And there are some IFTTT stuff you can do as well.

 

it should be something like that but how sonos cloud knows my current speaker’s IP address as it is dynamic? 

Badge

That’s not my understanding. The cloud services are, to my knowledge, provided not by Sonos, but by Amazon and Google, for their respective voice systems. Sonos just points to whichever cloud service you’ve set up as a voice control, in exactly the same manner as a streaming music source.

At least, that’s the way I perceive it to work. Feel free to point at anything that suggests I’m incorrect. 

Tho I guess it first connects to sonos cloud, no matter to each one it connects first. the issue is how the cloud/server knows the current IP address of my speaker with dhcp setup? it might change many times per days. 

There is no ‘Sonos Cloud’.

Your controller, when you start it and connect to your WiFi, gets an IP address from your router. When you play ‘from this device’, the controller communicates that IP address to the speakers, which then use it to get the data from your device. When you leave your WiFi, that IP address can and likely will change. Again, this is why you can’t add music from a mobile device in a playlist inside the Sonos controller. If that mobile device is ‘missing’ from the WiFi, how would another person be able to play that music?

It would be odd for the speakers to work differently for streaming from your device as opposed to streaming from an internet source such as Spotify or Apple Music. Significantly more coding effort. It makes more sense to use the existing process to do a similar thing. 

However, this is conjecture on my part. Folks like @controlav would have better knowledge, he has written his own controllers for Sonos, and is likely much more familiar with the way the internal system works than I. 

I think we are talking about two different things here.   What Bruce ( @Airgetlam ) is refering to is how a speaker retrieves the audio stream it’s been instructed to play.  Streaming services is obvious, as the speakers know the address to go retrieve the stream.  He’s expanding the discussion to explain that ‘play audio from this device’ isn’t really different.  The speaker is on the same network as the phone/tablet controlling device and therefore knows where to access the music files on your phone/tablet.

I don’t think that was the original question though.  It’s not about how the speaker retrieves an audio stream, but how a device that is not currently connected to your home network can control Sonos speakers (really any IOT device) inside your network when dynamic IP address assignments are used.  This really a more generic question as this is a function of most IOT devices.  How do you control your lights, your alarms, your cameras, etc when outside of the network?

It’s my understanding that Sonos, like any other company that sells IOT devices, maintains a server that receives control commands from your phone/tablet,etc or other services (Amazon Alexa) and is then responsible for sending the command on to the appropriate device inside your home network.  In this way, your phones/tablets and other services don’t need to individually track the IP address of your IOT devices, they only contact the cloud server that tracks that information.

That still doesn’t answer how a Sonos cloud, or any other IOT related cloud, is able to know the internal dynamic IP address of the devices in your home network.  I am not exactly sure, though I suspect that your devices will periodically ping the service with it’s current IP settings.  Someone more versed in networks would need to answer.  I did try and do some google research on it, but too many companies trying to sell IOT and cloud computing to sift through.

Userlevel 7
Badge +21

@Airgetlam is mostly correct in his description.

However there is a Sonos Cloud. It is used for voice control integration and things like Spotify Direct, where music services can directly control what the devices in your home play (though only a few of the ~70 supported services do this). It is also used for a few admin-level Settings, such as line-in level and explicit track permissions, as well as storage for diagnostic data.

For details on how a music service can send music directly to a speaker in your home, skipping your controller, read https://developer.sonos.com/build/direct-control/ - the music services connects to the Sonos cloud with the credentials you entered via the Sonos login page in their app. The Sonos devices in your home “phone home” regularly and pick up any commands generated in the Sonos cloud for playback.

The Sonos Cloud is not necessary for local control of Sonos players, or to play music from a streaming service (which uses SMAPI, not the cloud API). Third party apps such as mine have no need for its features, and compared to the “local” APIs we use the cloud API is seriously deficient for use by a controller app: I know this because I created a prototype that did just that.

The Sonos devices in your home “phone home” regularly and pick up any commands generated in the Sonos cloud for playback.

 

How often is regularly?  If the cloud can’t initiate contact with the speakers, then the speakers would need to check with the cloud at a high frequency, correct?  Something around once a second or less?

Userlevel 7
Badge +21

The Sonos devices in your home “phone home” regularly and pick up any commands generated in the Sonos cloud for playback.

 

How often is regularly?  If the cloud can’t initiate contact with the speakers, then the speakers would need to check with the cloud at a high frequency, correct?  Something around once a second or less?


I don’t know, but if I did it I would use a long-running http request or SignalR, which has minimal overhead and isn’t really timer-based as my wording implied.

Badge

@Airgetlamis mostly correct in his description.

However there is a Sonos Cloud. It is used for voice control integration and things like Spotify Direct, where music services can directly control what the devices in your home play (though only a few of the ~70 supported services do this). It is also used for a few admin-level Settings, such as line-in level and explicit track permissions, as well as storage for diagnostic data.

For details on how a music service can send music directly to a speaker in your home, skipping your controller, read https://developer.sonos.com/build/direct-control/ - the music services connects to the Sonos cloud with the credentials you entered via the Sonos login page in their app. The Sonos devices in your home “phone home” regularly and pick up any commands generated in the Sonos cloud for playback.

The Sonos Cloud is not necessary for local control of Sonos players, or to play music from a streaming service (which uses SMAPI, not the cloud API). Third party apps such as mine have no need for its features, and compared to the “local” APIs we use the cloud API is seriously deficient for use by a controller app: I know this because I created a prototype that did just that.

 

Thanks @controlav for your reply and the link.

I am familiar with cloud and API request as I did some projects in python and flask.

The only thing that I could not figure it out is how Sonos servers/clouds can track the IP address of my Sonos speakers connected to my home WiFi with DHCP set up?

I do not think Sonos speakers continuously check the Sonos server for any commands as it increases the traffic and other issues.

Userlevel 7
Badge +14

The only thing that I could not figure it out is how Sonos servers/clouds can track the IP address of my Sonos speakers connected to my home WiFi with DHCP set up?

I do not think Sonos speakers continuously check the Sonos server for any commands as it increases the traffic and other issues.

Hey @Reezaro,

 

The Sonos cloud server doesn’t know or need your local IP addresses. Each speaker establishes a connection to a cloud endpoint and performs pings at regular intervals (I’m not sure of the frequency though, sorry!) to keep the connection alive. Commands from the cloud use this connection to send instructions to your system for whatever task is being performed. 

Your DHCP assigned local IP is only used by the speakers themselves and other devices connected to your local network, not by music services or cloud servers.

 
 
 
 
 
Badge

The only thing that I could not figure it out is how Sonos servers/clouds can track the IP address of my Sonos speakers connected to my home WiFi with DHCP set up?

I do not think Sonos speakers continuously check the Sonos server for any commands as it increases the traffic and other issues.

Hey @Reezaro,

 

The Sonos cloud server doesn’t know or need your local IP addresses. Each speaker establishes a connection to a cloud endpoint and performs pings at regular intervals (I’m not sure of the frequency though, sorry!) to keep the connection alive. Commands from the cloud use this connection to send instructions to your system for whatever task is being performed. 

Your DHCP assigned local IP is only used by the speakers themselves and other devices connected to your local network, not by music services or cloud servers.

 
 
Hey@James L. 
 
Thanks for your reply.
Tho I can understand the ping mechnism of each speakers, it is a little bit weird for me because it highly increases the data traffic on Sonos servers.
Also, I can’t underand how Sonos cloud sends command through that established conenction!!! I think the cloud needs a valid IP address to send commands back. Cloud you explain it a little bit more? 
Userlevel 5
Badge +9

When the sonos speaker communicates to the sonos cloud it opens a long running connection with the sonos cloud which will have very low impact as @controlav says. Once a connection is open between two devices they both know an ip address (endpoint) that enables data to flow between them. The endpoint at your end will normally  be the external ip adress of your router - this is assigned by your Internet provider.  When the sonos cloud “responds" on that connection it talks to the endpoint on your router. Your router keeps track of open connections and when a message arrives for your sonos device the router “knows" which device initiated the connection and forwards the message onto that device. 

Sonos therefore knows the ipaddress of your house, your external ip address which can change -  it is under the control of your provider.  Some never change some, less common, change quite regularly. If you use a vpn provider then the endpoint sonos sees is the vpn providers.

By default your home router prevents people from outside initiating connections so the Sonos cloud cannot initiate the connection, your sonos system initiates the connection probably at start up.

@James L. is right that the sonos cloud does not need your local address because it does not initiate the connection but once the connection is open it does know your routers address and despite what he says the message sent to the cloud  probably contains the ip address of the  speaker

As an aside the provider e.g. sonos can use the endpoint ip address to roughly get the physical location of your router. This is one of the ways that companies can target advertising.

Edited to make a correction about knowing local ip address

Badge

When the sonos speaker communicates to the sonos cloud it opens a long running connection with the sonos cloud which will have very low impact as @controlav says. Once a connection is open between two devices they both know an ip address (endpoint) that enables data to flow between them. The endpoint at your end will normally  be the external ip adress of your router - this is assigned by your Internet provider.  When the sonos cloud “responds" on that connection it talks to the endpoint on your router. Your router keeps track of open connections and when a message arrives for your sonos device the router “knows" which device initiated the connection and forwards the message onto that device. 

Sonos therefore knows the ipaddress of your house but not your speaker. Your external ip address can change, it is under the control of your provider.  Some never change some, less common, change quite regularly. If you use a vpn provider then the endpoint sonos sees is the vpn providers.

As an aside the provider e.g. sonos can use the endpoint ip address to roughly get the physical location of your router. This is one of the ways that companies can target advertising.

 

So as I understood, Sonos cloud can just track the IP until my router. The rest, is my router's task to automatically transfer data to my speakers? 

Userlevel 5
Badge +9

If you meant “ track the IP until my router changes its IP address” then yes ish. It is not really tracking there is an open connection rather like a telephone call and data can flow between both ends for as long as the connection is open. You are correct the router is responsible for transferring the data to the speaker.

If you look at my updated reply the Sonos cloud cannot initiate the connection. The speaker initiates the connection which it keeps open for a long time. I have no idea how long “long” is but whenever it reaches the end the speaker will just initiate a new connection.

Badge

If you meant “ track the IP until my router changes its IP address” then yes ish. It is not really tracking there is an open connection rather like a telephone call and data can flow between both ends for as long as the connection is open. You are correct the router is responsible for transferring the data to the speaker.

If you look at my updated reply the Sonos cloud cannot initiate the connection. The speaker initiates the connection which it keeps open for a long time. I have no idea how long “long” is but whenever it reaches the end the speaker will just initiate a new connection.

So you mean there is always a connection between speakers and cloud 24/7 ? 

Userlevel 7
Badge +21

So you mean there is always a connection between speakers and cloud 24/7 ? 

At minimum a socket will be open between the two, yes. I am guessing the same channel is also used to receive update notifications.

Why are you so concerned about this? Every cloud-connected device in your home that can be controlled by an app when not on your Wifi (eg Ring) works similarly.

Badge

So you mean there is always a connection between speakers and cloud 24/7 ? 

At minimum a socket will be open between the two, yes. I am guessing the same channel is also used to receive update notifications.

Why are you so concerned about this? Every cloud-connected device in your home that can be controlled by an app when not on your Wifi (eg Ring) works similarly.

I got it.

I am not concerned, it is just interesting for me. I had never had a cloud-based device and Sonos speakers were the first ones. Happy to learn. 

Thanks   @James L. @controlav  and others

Reply