spotify-web-playback-sdk-example icon indicating copy to clipboard operation
spotify-web-playback-sdk-example copied to clipboard

Please clarify life-cycle considerations of the Player object

Open stippi opened this issue 1 year ago • 4 comments

Hi,

First of all, thanks for making the SDK available and the great documentation and this example!! I was able the get everything working rather quickly. However, a few questions arise which I cannot find answers to anywhere:

When my Single Page Application is open for a longer time, the player connection (usually when idle, i.e. not streaming, but also sometimes when streaming) will become broken eventually. That means, even though I don't receive any "not ready" event from the player, I cannot start streaming again. Some of the symptoms will be (a random combination of them):

  • I will see "502 Bad Gateway" errors for the web socket in the log which seem to come from the Web Playback SDK Javascript.
  • Trying to start playback may produce a "404" which refers to the Device ID that the Player instance was assigned, as the device will no longer be in the device list on the server side.
  • Trying to call disconnect() on the Player instance will just hang.
  • Simply calling connect() again may be "successful", but the connection is actually still broken.

Please note that I periodically refresh the access token, and that I give the Player instance a callback implementation that will always use the current access token.

I am perfectly fine with the Player instance becoming stale after a while. What I need is a way to 1) become aware of it reliably (like by receiving the "not ready" event, and 2) being able to recover from that without reloading the page.

I also observed that multiple instances of my application may be registered at the Spotify servers concurrently. For example, when I reload the page, I get a new device ID, and the previous instance may still exist in the Spotify devices list.

Thanks a lot for any clarification!

stippi avatar Mar 01 '24 10:03 stippi

hi @stippi

i am running into the same problems. have you found any way to fix it?

SonOfCrypto avatar Aug 09 '24 14:08 SonOfCrypto

i am running into the same problems. have you found any way to fix it?

Hi, unfortunately not yet. I added logging to the mentioned callback passed to the player instance for getting an access token. It will fetch a new token on demand, for example when I skip to the next or previous song using the methods provided by the player instance. In this case, playback resumes even after a long time of my page just idling. My plan was to investigate whether the device ID of the player will appear again in the Spotify API that lists devices. If yes, maybe it's an idea to perform some function on the player that causes it to refresh the token in order to repair the connection and "reanable" the device ID for calls to the API.

stippi avatar Aug 09 '24 22:08 stippi

Thank you for your detailed answer @stippi. It helped me a lot and I think I managed to fix it based on your feedback. I have updated my code and I now request a new token every 5 minutes instead of the 60 minutes spotify suggests. Since the update my player runs smoothly.

SonOfCrypto avatar Aug 25 '24 12:08 SonOfCrypto

I'm happy to hear that. Is your code open source so I could have a look? Thanks!

stippi avatar Aug 26 '24 06:08 stippi