This is just an example of establishing an authenticated socket connection for audiobookshelf.
On the server a user can have many socket clients. This means you can login as the same user in multiple browser windows and they will all receive events for that user.
The demo works like this:
- Initialize the socket client
- Login to the server using the audiobookshelf API
- Emit a socket event 'auth' with the users API token
Once that is done the user will be linked to that socket connection and all events emitted from the server intended for that user will be received by the client.
Note: if the socket gets disconnected and re-connects it will have a new socket id and needs to emit the 'auth' event again.
Node v16 or higher is required.
git clone https://github.com/advplyr/abs-socket-client-demo.git
cd abs-socket-client-demo
npm install
First update the config.json
with your server info.
For example if your server is running https://abs.example.com
then your config would look like:
{
"protocol": "https",
"hostname": "abs.example.com",
"port": 443,
"username": "root",
"password": ""
}
Replace username/password with yours.
To run the demo make sure you are in the abs-socket-client-demo
folder then..
npm run start
If you leave this running and login to the same user in a browser then you will see in the console all the events being sent for that user.
To test this you can update a library item in the browser and you should see an item_updated
event in the console with the full library item object.