plug-socket
Simple plug.dj WebSocket EventEmitter library.
Usage
const plugSocket = const authToken = '(...)' // get one by GET-ing https://plug.dj/_/auth/token let socket = // events will be fired on "socket" for every incoming plug.dj messagesocketsocketsocket
API
let socket = plugSocket(?authToken)
Sets up a WebSocket connection to plug.dj. If the auth token is given, it also sends an "auth" message once the connection is open. Otherwise, you'll have to send that yourself.
socket
is a WebSocket connection instance
with a few extra methods and a bunch of extra events.
socket.auth(authToken)
Sends an auth token. You should only call this once, and only if you did not
pass one to the plugSocket()
call.
You can obtain an auth token by logging in to plug.dj using something like plug-login, or by manually sending a GET request to https://plug.dj/_/auth/token.
const plugSocket = // Using `plug-login`'s authToken option:const plugLogin = // Or manually, with a cookie stored in `mySessionCookie`:const got =
socket.chat(message)
Sends a chat message to the current room. Make sure to join a room first by sending a POST request to https://plug.dj/_/rooms/join:
got
Events
Aside from the standard WebSocket events, plug-socket
also emits different
events for all plug.dj message types. These are:
"ack" "advance" "ban" "banIP" "chat" "chatDelete" "djListCycle" "djListLocked" "djListUpdate" "earn" "sub" "cash" "gift" "floodChat" "floodAPI" "friendRequest" "friendAccept" "gifted" "grab" "killSession" "modBan" "modAddDJ" "modRemoveDJ" "modMoveDJ" "modMute" "modSkip" "modStaff" "nameChanged" "nameChangedRoom" "notify" "playlistCycle" "plugMaintenance" "plugMaintenanceAlert" "plugMessage" "plugUpdate" "rateLimit" "roomNameUpdate" "roomDescriptionUpdate" "roomWelcomeUpdate" "roomMinChatLevelUpdate" "skip" "userJoin" "userLeave" "userUpdate" "vote"
Plug.dj events receive two arguments, param
and slug
. param
is usually
an object, or undefined
for some events. The slug
parameter contains the
current room slug or "dashboard". When you switch rooms, sometimes you'll keep
receiving a few events from your previous room, so the slug
parameter allows
you to filter those. You won't have to care for it if your app doesn't switch
rooms much.
socket
You can also handle every plug.dj event by adding an "action" listener:
socket
Most events are documented in more detail in the PlugCommunity Documentation repository.
Tests
Tests use mocha
. All tests depend on plug.dj being online and reachable, so
you might get test failures if it's slow, or in maintenance mode, or shut down
for a few months.