All Projects → lrholmes → spotify-connect-ws

lrholmes / spotify-connect-ws

Licence: other
A Socket.IO plugin for interfacing with Spotify's Connect API using WebSockets.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to spotify-connect-ws

Spotify.ahk
An AutoHotkey wrapper for the Spotify web API designed to allow control over Spotify's internal volume slider and provide various other functionality.
Stars: ✭ 50 (-18.03%)
Mutual labels:  spotify-api
SpotMusicGen
A Program that creates a Spotify playlist from a YouTube Playlist
Stars: ✭ 47 (-22.95%)
Mutual labels:  spotify-api
react-chat-client
A simple chat client built in React for communicating with the node-multi-server-chat example
Stars: ✭ 24 (-60.66%)
Mutual labels:  socket-io
spiced-final-project
Career explorer platform developed in React.js in 6 days.
Stars: ✭ 14 (-77.05%)
Mutual labels:  socket-io
chokidar-socket-emitter
a simple chokidar watcher which emits events to all connected socket.io clients
Stars: ✭ 28 (-54.1%)
Mutual labels:  socket-io
RIACharRoom
富媒体游戏聊天室,挂coding上了
Stars: ✭ 15 (-75.41%)
Mutual labels:  socket-io
ChartTheStockMarket
🔥 📈 💸 Chart the Stock Market - FCC Challenge
Stars: ✭ 39 (-36.07%)
Mutual labels:  socket-io
live-graph
Simple live graph with Flask and SocketIO
Stars: ✭ 17 (-72.13%)
Mutual labels:  socket-io
spotify-release-gun
A python tool that informs about new releases of the artists you follow.
Stars: ✭ 17 (-72.13%)
Mutual labels:  spotify-api
playlists
💽 An archive of my Spotify playlists.
Stars: ✭ 34 (-44.26%)
Mutual labels:  spotify-api
2019-01
👨‍💻👩‍💻Boolean Avengers : 개발 상식을 점검할 수 있는 웹 시리어스 게임 서비스
Stars: ✭ 38 (-37.7%)
Mutual labels:  socket-io
titanium-socketio
Use the native Socket.io SDK's with Axway Titanium.
Stars: ✭ 25 (-59.02%)
Mutual labels:  socket-io
order-service
一个基于vuejs,reactjs,nodejs,socket.io的服务系统
Stars: ✭ 22 (-63.93%)
Mutual labels:  socket-io
uni-chat
uni-app + vue3.0 + typescript + vue-cli 仿手机QQ聊天 qq表情包
Stars: ✭ 55 (-9.84%)
Mutual labels:  socket-io
spotifytrack
A personal homepage showing users' top songs and artists, providing a shareable link that they can use to show it off to friends.
Stars: ✭ 48 (-21.31%)
Mutual labels:  spotify-api
Spreen
Now use any device with a web browser as a second screen for your laptop or PC. No cables. No internet. No software installation. Just Spreen, that's it.
Stars: ✭ 29 (-52.46%)
Mutual labels:  socket-io
rAudio-1
Raspberry Pi audio player: AirPlay, Audio CD, Bluetooth, DAB radio, DSP, Internet rafio, Multi-room, Spotify Connect, UPnP
Stars: ✭ 151 (+147.54%)
Mutual labels:  spotify-connect
meditation-plus-angular
DEVELOPMENT MOVED TO
Stars: ✭ 15 (-75.41%)
Mutual labels:  socket-io
nodebb-theme-oxide
Theme based on Github for NodeBB.
Stars: ✭ 23 (-62.3%)
Mutual labels:  socket-io
uno-multiplayer
uno multiplayer web app game based on socket io and nodejs
Stars: ✭ 27 (-55.74%)
Mutual labels:  socket-io

WebSockets for Spotify Connect API

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, 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.

Usage

This package has been developed to work with an Express + Socket.IO server environment.

If you want to skip the server setup and go straight to working with your app, use this testing url in the meantime: https://spotify-connect-ws.herokuapp.com/connect

Examples

I have made a couple of example projects that show how your server and client apps might look when using this package.

Client (React.js): https://github.com/lrholmes/spotify-connect-ws-react-example

Server: https://github.com/lrholmes/spotify-connect-ws-server-example

Server:

npm install spotify-connect-ws --save
# or
yarn add spotify-connect-ws
import express from 'express'
import socketio from 'socket.io'
import connectSocket from 'spotify-connect-ws'

const app = express()
const server = app.listen(process.env.PORT || 3001)

const io = socketio(server)
io.of('connect').on('connection', connectSocket)

Client:

npm install socket.io-client --save
# or
yarn add socket.io-client
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' })

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.

Events

Received Events

These events are used in combination with on() to receive changes to the player state.

Example:

io.on('track_change', track => {
  // update state/store with new 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.

track_change: Received after a track change is detected, along with the Spotify Track object for the new track.

playback_started: Indicates playback starting (both when a new track starts and when a paused track resumes playback)

playback_paused

device_change: A change to the active playback device has occured. The new Device is sent back with this event.

volume_change: Volume has been changed - the volume percent is sent back.

track_end: The current track has ended. This event is detected on the serverside by checking if the track is has less than two seconds of playback remaining. This is reliable in my testing, but there is probably a better way of checking for track end.

Sent Events

These are used to trigger playback events.

Example:

io.emit('play', { id: '5Y17vKO1JtfnxIlM4vNQT6' })

initiate: Accepts an object with required accessToken parameter. This event begins watching the player state and is required before using any of the other events.

play: This event accepts an optional object representing the parameters for the Connect API (uris, context_uri, offset), but additionally accepts id, representing a Spotify Track ID, that will play that track. If no object is provided, the server will attempt to resume playback.

resume: Resume playback.

pause: Pause playback.

seek: Seek to position provided as argument (target position in milliseconds).

next_track: Skip to next track in queue

previous_track: Go to previous track in play history

set_volume: Set volume to integer representing percentage provided as argument

transfer_playback: Transfer playback to another device, accepts an object with following params: id, the device ID, and play, which defaults to false but if true will force playback on the new device.

access_token: This will set the socket's access token. If you are refreshing your token, ensure to send this event after.

Contributing

Please contribute with any changes or fixes you'd like to see! Especially if you have any experience with Socket.IO, I'm sure that I've probably made some mistakes here or there.

Some things that I would like to add soon:

  • The rest of the Player options (shuffle, repeat)
  • Tests
  • The ability to manage devices (events for new devices detected)

Longer term, it might be possible to dynamically adjust the poll time when there are more users connected, but first we'll need to know at what point rate limits become an issue.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].