npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

plug-socket

v1.0.0

Published

Simple plug.dj WebSocket server interaction.

Downloads

32

Readme

plug-socket

Simple plug.dj WebSocket EventEmitter library.

NPM

Usage

const plugSocket = require('plug-socket')
const authToken = '(...)' // get one by GET-ing https://plug.dj/_/auth/token

let socket = plugSocket(authToken)
// events will be fired on "socket" for every incoming plug.dj message
socket.on('chat', msg => log(`<${msg.un}>  ${msg.message}`))
socket.on('userJoin', msg => log(`  * ${msg.un} joined the room`))
socket.on('plugMaintenanceAlert', () => log('#ded soon… ×.×'))

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 = require('plug-socket')

// Using `plug-login`'s authToken option:
const plugLogin = require('plug-login')
plugLogin(myEmail, myPassword, { authToken: true }).then((result) => {
  const sock = plugSocket(result.token)
})

// Or manually, with a cookie stored in `mySessionCookie`:
const got = require('got')
got('https://plug.dj/_/auth/token', {
  json: true,
  // Make
  headers: { cookie: mySessionCookie }
}).then((response) => {
  const authToken = response.body.data[0]
  const sock = plugSocket(authToken)
})

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.post('https://plug.dj/_/rooms/join', {
  json: true,
  headers: {
    cookie: mySessionCookie,
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    slug: 'my-room-slug'
  })
}).then((response) => { /* joined! */ })

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.on('chat', (param, slug) => {
  log(`${slug}: receiving`, param)
})

You can also handle every plug.dj event by adding an "action" listener:

socket.on('action', (type, param, slug) => {
  // `type` is one of the events listed above.
  log(`${slug}: receiving a "${type}" event with`, param)
})

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.

License

MIT