spotify-connect-ws
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -37,3 +37,3 @@ 'use strict'; | ||
socket.on('disconnect', function () { | ||
clearInterval(socket.poll); | ||
socket.poll = function () {}; | ||
}); | ||
@@ -73,11 +73,9 @@ | ||
socket.hasNotifiedTrackEnd = false; | ||
} else { | ||
// track is the same, check if it has been scrubbed | ||
if (playerState.is_playing) { | ||
var negativeProgress = playerState.progress_ms > socket.playerState.progress_ms + C.HAS_SCRUBBED_THRESHOLD; | ||
var positiveProgess = playerState.progress_ms < socket.playerState.progress_ms - C.HAS_SCRUBBED_THRESHOLD; | ||
if (negativeProgress || positiveProgess) { | ||
socket.emit('seek', playerState.progress_ms, playerState.timestamp); | ||
} | ||
} | ||
} else {} | ||
// check if the track has been scrubbed | ||
var negativeProgress = playerState.progress_ms > socket.playerState.progress_ms + C.HAS_SCRUBBED_THRESHOLD; | ||
var positiveProgess = playerState.progress_ms < socket.playerState.progress_ms - C.HAS_SCRUBBED_THRESHOLD; | ||
if (negativeProgress || positiveProgess) { | ||
socket.emit('seek', playerState.progress_ms, playerState.timestamp); | ||
} | ||
@@ -84,0 +82,0 @@ if (playerState.is_playing !== socket.playerState.is_playing) { |
{ | ||
"name": "spotify-connect-ws", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A WebSockets solution for Spotify's Connect API made using socket.io.", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -1,14 +0,11 @@ | ||
WebSockets for Spotify Connect API | ||
=========== | ||
# WebSockets for Spotify Connect API | ||
A Socket.IO plugin that enables interfacing with Spotify's Connect API using WebSockets. | ||
A Socket.IO plugin that enables interfacing with Spotify's Connect API using WebSockets. | ||
The advantage of using this package is that it takes away the need for client-side polling (and diffing). By connecting the client to the server with WebSockets, the server will handle all of the polling and diffing, and the client will simply be notified whenever there is a change to the state of the player. This makes the client side code a lot cleaner and simpler. | ||
It should be noted that this project does not fully solve the problems discussed in [this issue](https://github.com/spotify/web-api/issues/492), in that there is still polling taking place (one request per second right now) and therefore hitting rate limits is a possibility. | ||
It should be noted that this project does not fully solve the problems discussed in [this issue](https://github.com/spotify/web-api/issues/492), in that there is still polling taking place (one request per second right now) and therefore hitting rate limits is a possibility. If you do hit the rate limit using this plugin, please let me know how many concurrent users caused it, because I would like to integrate functionality that automatically throttles the poll rate based on the number of concurrent websocket connections. | ||
### Important Note | ||
As of right now, this package is the result half a day's work (inc. learning WebSockets), so this is untested, and probably unready for production applications. Having said that, I already _really_ like this method of interacting with the Connect API so will continue to work on this project until Spotify can provide their own solution. Hopefully you will be able to help out too. | ||
### Usage | ||
### Usage | ||
This package has been developed to work with an Express + Socket.IO server environment. | ||
@@ -19,2 +16,3 @@ | ||
Server: | ||
```bash | ||
@@ -36,6 +34,6 @@ npm install spotify-connect-ws --save | ||
io.of('connect').on('connection', connectSocket) | ||
``` | ||
Client: | ||
```bash | ||
@@ -48,11 +46,12 @@ npm install socket.io-client --save | ||
```js | ||
import openSocket from 'socket.io-client' | ||
const io = openSocket('/connect') | ||
// or if using testing url | ||
const io = openSocket('https://spotify-connect-ws.herokuapp.com/connect') | ||
import openSocket from 'socket.io-client' | ||
const io = openSocket('/connect') | ||
// or if using testing url | ||
const io = openSocket('https://spotify-connect-ws.herokuapp.com/connect') | ||
io.emit('initiate', { accessToken: 'access token' }) | ||
io.emit('initiate', { accessToken: 'access token' }) | ||
``` | ||
``` | ||
### How it works | ||
To start watching the player for changes, use `io.emit('initiate', { accessToken })`. If successful, the first event receieved by the client, named `initial_state` will provide the full Player object from Spotify. You should use this to set up any views or state needed for your app. After this event, your client will receive events based on changes to the Player's state (e.g. playback paused, track changed). All of these events are listed below. | ||
@@ -63,5 +62,7 @@ | ||
#### Received Events | ||
These events are used in combination with `on()` to receive changes to the player state. | ||
Example: | ||
```js | ||
@@ -73,2 +74,4 @@ io.on('track_change', track => { | ||
`initial_state`: This event is received once, after initiating the connection. It contains the full Player object. | ||
`connect_error`: Any errors encountered on the server will be sent back to the client here. | ||
@@ -89,5 +92,7 @@ | ||
#### Sent Events | ||
These are used to trigger playback events. | ||
Example: | ||
```js | ||
@@ -117,3 +122,2 @@ io.emit('play', { id: '5Y17vKO1JtfnxIlM4vNQT6' }) | ||
### Contributing | ||
@@ -124,2 +128,3 @@ | ||
Some things that I would like to add soon: | ||
* The rest of the Player options (shuffle, repeat) | ||
@@ -126,0 +131,0 @@ * Tests |
@@ -43,3 +43,3 @@ import { | ||
socket.on('disconnect', () => { | ||
clearInterval(socket.poll) | ||
socket.poll = () => {} | ||
}) | ||
@@ -81,19 +81,14 @@ | ||
} else { | ||
// track is the same, check if it has been scrubbed | ||
if (playerState.is_playing) { | ||
const negativeProgress = | ||
playerState.progress_ms > | ||
socket.playerState.progress_ms + C.HAS_SCRUBBED_THRESHOLD | ||
const positiveProgess = | ||
playerState.progress_ms < | ||
socket.playerState.progress_ms - C.HAS_SCRUBBED_THRESHOLD | ||
if (negativeProgress || positiveProgess) { | ||
socket.emit( | ||
'seek', | ||
playerState.progress_ms, | ||
playerState.timestamp | ||
) | ||
} | ||
} | ||
} | ||
// check if the track has been scrubbed | ||
const negativeProgress = | ||
playerState.progress_ms > | ||
socket.playerState.progress_ms + C.HAS_SCRUBBED_THRESHOLD | ||
const positiveProgess = | ||
playerState.progress_ms < | ||
socket.playerState.progress_ms - C.HAS_SCRUBBED_THRESHOLD | ||
if (negativeProgress || positiveProgess) { | ||
socket.emit('seek', playerState.progress_ms, playerState.timestamp) | ||
} | ||
if (playerState.is_playing !== socket.playerState.is_playing) { | ||
@@ -100,0 +95,0 @@ // play state has changed |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
127
8
111149
527