bencevans / node-sonos

🔈 Sonos Media Player Interface/Client
https://www.npmjs.com/package/sonos
MIT License
703 stars 147 forks source link

Device not found on deployed site using node-sonos #520

Open felixdasgupta opened 2 years ago

felixdasgupta commented 2 years ago

DeviceDiscovery doesn't work for a deployed web application. No Sonos Device is found on the network.

Expected Behavior

A Device should be discovered when searching for the Device in this instance using DeviceDiscovery.

Current Behavior

Currently works in my local environment, but I've deployed my application. When I run this on a chrome browser the device is never discovered.

My application uses a Next.JS + React stack with express.

Possible Solution

Not 100% clear on how this works and I am definitely new to this. Do I need to set an environment variable here? How would I configure the production version to properly discover devices connected to an account etc.

This is more of a question, not a proposed solution, but I would appreciate all the help I can get here. Not clear on how DeviceDiscovery is actually working without being tied to any account or auth.

Sample code or executed example

This piece of code never works in the production instance.

const findDevice = DeviceDiscovery({ timeout });

findDevice.on("DeviceAvailable", async sonosDevice => {
      ....
})

Versions (and Environment)

Node version: "14.x" node-sonos version: "^1.14.1" OS: MacOS

felixdasgupta commented 2 years ago

Just following up here, any idea on how I could better discover devices on a hosted application that uses node-sonos?

It seems to be searching for devices but it never seems to discover it properly and I don't see any kind of error message. Would greatly appreciate some advice on this.

pascalopitz commented 2 years ago

If the device you run the app on isn't in the same network as the players it won't work.

Device discovery works by making an UPNP announce Multicast telling the devices where to log notify UPNP calls to.

On Fri, 31 Dec 2021, 10:09 Felix, @.***> wrote:

Just following up here, any idea on how I could better discover devices on a hosted application that uses node-sonos?

It seems to be searching for devices but it never seems to discover it properly and I don't see any kind of error message. Would greatly appreciate some advice on this.

— Reply to this email directly, view it on GitHub https://github.com/bencevans/node-sonos/issues/520#issuecomment-1003213451, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACCK7BU523CV44IS6K722TUTTUS7ANCNFSM5KVSN3SQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

felixdasgupta commented 2 years ago

It is running on the same wifi network yes, but still having issues discovering the device. It keeps searching but never finds it.

Any idea on how to force a discover on the same network using a groupId for a specific device potentially?

felixdasgupta commented 2 years ago

Thanks in advance! @pascalopitz

pascalopitz commented 2 years ago

On unofficial sonos controller for linux for example, you can use the IP address and add manually via menu dialog

On Sat, 1 Jan 2022, 05:07 Felix, @.***> wrote:

Thanks in advance! @pascalopitz https://github.com/pascalopitz

— Reply to this email directly, view it on GitHub https://github.com/bencevans/node-sonos/issues/520#issuecomment-1003433315, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACCK7A24ENRXHBQLKCAD53UTXZ7PANCNFSM5KVSN3SQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

felixdasgupta commented 2 years ago

Does that mean passing an IP Address and a port on DeviceDiscovery({ port: PUBLIC_IP_ADDRESS + ":80" })?

Would greatly appreciate a code snippet or a link... this is an express + next.js project, but not quite clear on how to leverage the IP Address to find the device manually.

pascalopitz commented 2 years ago

No, it would basically be the same as adding a known device:

const { Sonos } = require('sonos')

const device = new Sonos('192.168.1.56');

From there on you can query the topology etc

On the alpha release, see: https://github.com/bencevans/node-sonos/blob/bea996efa92b8519dc975596424f0c615dec6de7/lib/sonos.js#L376

felixdasgupta commented 2 years ago

@pascalopitz Appreciate the help so far! But having some issues getting this to work...

This is what my code looks like:

const device = new Sonos(address);
const zonesArray = await device.getAllGroups();

And this is the error I am getting when it's run:

error - unhandledRejection: Error: connect ECONNREFUSED {{address}}:1400
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)

Unfortunately it seems like the connection is being refused... not sure how to get around this. Have you ever encountered anything like this?

pascalopitz commented 2 years ago

That is very much depending on your individual network topology, so can't comment

svrooij commented 2 years ago
  1. the browser running your web application doesn't support UDP sockets. So multicast won't work for discovery
  2. The Sonos speakers don't support cors so you'll need to make a "backend" application, that is responsible on receiving messages from your website and the forwarding it to the Sonos devices

Something like:

react app talks to http server (to build) which talks to sonos. @pascalopitz did something similar for his Linux Sonos app

felixdasgupta commented 2 years ago

@svrooij Unfortunately I am using a backend server/app to try and connect to the Sonos Network using node.js/express.

Will try and debug, appreciate the help guys!