plug.dj has an API to empower Javascript developers to write extensions. The API is an event dispatcher that triggers after various events occur that developers might want to listen for. It’s simple to use and what you do with it is up to your imagination!
OVERVIEW
There are two different parts to the API. There are functions that return various data, and there is an event dispatching engine. API is a reference on the global scope of the document so you can access it easily from your extensions.
DATA
The data calls are as follows:
API.getAudience()
Returns an Array of user objects of all the users in the audience (not including DJs).
API.getDJs()
Returns an Array of user objects of the DJs. The first DJ in the array in the current DJ, and the rest are in order to the last DJ.
API.getUsers()
Returns an Array of user objects of the DJs and audience. The first DJ in the array in the current DJ, and the rest are in order to the last DJ, and the audience is sorted by alphanumeric.
API.getUser(id)
Returns the user object of a specific user.
API.getSelf()
Returns the user object of the logged in user.
API.getStaff()
Returns an Array of user objects of the room’s staff members that are currently in the room. Each user has property “permission” which is an integer between 1-5. 5 = Host, 4 = Co-Host, 3 = Manager, 2 = Bouncer, 1 = Featured DJ.
API.getAdmins()
Returns an Array of user objects of the Admins currently in the room.
API.getAmbassadors()
Returns an Array of user objects of the Ambassadors currently in the room.
API.getHost()
Returns the user object of the room host if they are currently in the room. Null otherwise.
API.getMedia()
Returns the media object of the current playing media.
API.getWaitList()
Returns an Array of user objects of users currently on the wait list.
API.getRoomScore()
Returns a room score object with the properties positive, negative, curates, and score.
API.sendChat(message)
Send a chat message.
API.waitListJoin()
Joins the wait list.
API.waitListLeave()
Leaves the wait list.
API.moderateForceSkip()
If you have permission, this will force skip the current DJ.
API.moderateAddDJ(id)
If you have permission, adds a user to the dj booth or wait list by passing that user’s id. Users who do not have an active playlist with one item in it cannot be added.
API.moderateRemoveDJ(id)
If you have permission, removes a DJ from the booth or wait list by passing that user’s id.
API.moderateKickUser(id, reason)
If you have permission, kicks a user from the room for 60 minutes. You can include a reason message to avoid showing the dialog.
API.moderateBanUser(id, reason)
If you have permission, permanently bans a user from the room forever. You can include a reason message to avoid showing the dialog.
API.moderateDeleteChat(cid)
If you have permission, delete an offensive chat message by its chat id.
API.moderateSetRole(userID, permission)
If you have permission, you can set another user’s permission level. Valid values for permission are 0-4. 4 = Co-Host, 3 = Manager, 2 = Bouncer, 1 = Featured DJ, 0 = None.
EVENTS
To add or remove an event listener, pass the name of the event and the function that will be called when the event occurs.
API.addEventListener(event, callback);
API.removeEventListener(event, callback);
These are all the events that the API dispatches. The event names are all on the API object. Here’s an explanation of each one and what is passed to the callback function.
CHAT
USER_SKIP
USER_JOIN
USER_LEAVE
USER_FAN
FRIEND_JOIN
FAN_JOIN
VOTE_UPDATE
CURATE_UPDATE
ROOM_SCORE_UPDATE
DJ_ADVANCE
DJ_UPDATE
VOTE_SKIP
MOD_SKIP
WAIT_LIST_UPDATE
CHAT
This is called when an incoming chat arrives. It passes a chat object.
USAGE
API.addEventListener(API.CHAT, callback);
function callback(data)
{
data.type
// "message", "emote", "moderation", "system"
data.from
// the username of the person
data.fromID
// the user id of the person
data.message
// the chat message
data.language
// the two character code of the incoming language
}
———————————————————————————
USER_SKIP
This is called when the current DJ skips their own turn. It passes the user object of the dj who skipped.
USAGE
API.addEventListener(API.USER_SKIP, callback);
function callback(user)
{
alert(user.username + " skipped their turn");
}
———————————————————————————
USER_JOIN
This is called when a user joins the room. It passes a user object.
USAGE
API.addEventListener(API.USER_JOIN, callback);
function callback(user)
{
alert(user.username + " joined the room");
}
———————————————————————————
USER_LEAVE
This is called when a user leaves the room. It passes a user object.
USAGE
API.addEventListener(API.USER_LEAVE, callback);
function callback(user)
{
alert(user.username + " left the room");
}
———————————————————————————
USER_FAN
This is called when somebody becomes your fan. It passes a user object.
USAGE
API.addEventListener(API.USER_FAN, callback);
function callback(user)
{
alert(user.username + " is now your fan");
}
———————————————————————————
FRIEND_JOIN
This is called when somebody you are a fan of joins the room. It passes a user object.
USAGE
API.addEventListener(API.FRIEND_JOIN, callback);
function callback(user)
{
alert("Your friend " + user.username + " just joined the room");
}
———————————————————————————
FAN_JOIN
This is called when somebody who is your fan but you are not a fan of joins the room. It passes a user object.
USAGE
API.addEventListener(API.FAN_JOIN, callback);
function callback(user)
{
alert("Your fan " + user.username + " just joined the room");
}
———————————————————————————
VOTE_UPDATE
This is called when somebody in the room (including you) votes. It passes an object with a user object and the vote, -1 for negative, 1 for positive.
USAGE
API.addEventListener(API.VOTE_UPDATE, callback);
function callback(obj)
{
var vote = obj.vote == 1 ? "woot" : "meh";
alert(obj.user.username + " voted " + vote);
}
———————————————————————————
CURATE_UPDATE
This is called when somebody in the room (including you) adds what is currently playing to their collection. It passes the user object who added it.
USAGE
API.addEventListener(API.CURATE_UPDATE, callback);
function callback(obj)
{
var media = API.getMedia();
alert(obj.user.username + " added " + media.author + " - " + media.title);
}
———————————————————————————
ROOM_SCORE_UPDATE
This is called whenever the room score changes and it passes a room score object with the properties positive, negative, curates, and score.
USAGE
API.addEventListener(API.ROOM_SCORE_UPDATE, callback);
function callback(obj)
{
alert(obj.positive + " woots, " + obj.negative + " mehs, " + obj.curates + " adds, score=" + obj.score);
}
———————————————————————————
DJ_ADVANCE
This is called when the dj advances to the next play. It passes an object with the array of user objects and the current media object. The first DJ in the array is the current DJ, and the remaining ones are waiting to play in that order. This is also called when the last DJ leaves the booth, in which case it passes null.
USAGE
API.addEventListener(API.DJ_ADVANCE, callback);
function callback(obj)
{
if (obj == null) return; // no dj
var str = "";
var currentDJ = obj.dj;
str += currentDJ.username;
var total = currentDJ.djPoints + currentDJ.listenerPoints + currentDJ.curatorPoints;
str += " points: " + total;
str += ", fans: " + currentDJ.fans;
str += " || " + obj.media.author + " - " + obj.media.title;
alert(str);
}
———————————————————————————
DJ_UPDATE
This is called when the djs in the booth change, whether it somebody joins, leaves, etc. It returns an array of user objects of the DJs in order from playing to 5th position.
USAGE
API.addEventListener(API.DJ_UPDATE, callback);
function callback(djs)
{
var len = djs.length;
for (var i = 0; i < len; ++i)
{
console.log(djs[i]);
}
}
———————————————————————————
WAIT_LIST_UPDATE
This is called when the users in the wait list change, whether somebody joins, leaves, is removed, or is added to the booth (and so removed automatically). It returns an array of the user objects in order from beginning to last in the wait list.
USAGE
API.addEventListener(API.WAIT_LIST_UPDATE, callback);
function callback(users)
{
var len = users.length;
for (var i = 0; i < len; ++i)
{
console.log(users[i]);
}
}
———————————————————————————
VOTE_SKIP
This is called when the room votes down enough to skip the curent DJ. Null is passed.
USAGE
API.addEventListener(API.VOTE_SKIP, callback);
function callback()
{
alert("The room has voted to skip");
}
———————————————————————————
MOD_SKIP
This is called when a moderator force skips the current DJ. It passes the username of the moderator.
USAGE
API.addEventListener(API.MOD_SKIP, callback);
function callback(username)
{
alert(username + " has skipped the current DJ");
}
I wish I understood this and could write these extensions..
Can we get an API call for getAmbassadors()?
We’ll add this to our Feature Requests forum.
http://support.plug.dj/forums/20806957-feature-requests
Thanks for the input!
You need to make this as a smartphone app!
You probably don’t want to do this right now…
You need to open up the API so someone else can make this as a smartphone app!
This is a nice place to have where everyone are sitting alone, raving in their chairs. But it would rock in a real world party, where people can sign in as DJs and vote on whether they like the current song or not (much like they can now, if they are sitting at home in their chairs). No need for wild animation, all that’s needed is:
* chat (mostly there)
* Sign in as DJ + music controls.
* Voting
Some semi-online mode would be nice, where the phone doesn’t have to stay connected to keep rocking to the song. Maybe something akin to the Google “party mode”?
howabout a screen similar to the pop-out chat, they you could chat, vote and (with a little addition) join the waitlist.
Can we get a bit more info on how to use the API? For example, where is the JS file that contains definitions for the API object? How do we authenticate and specify room ID (ie, how to subscribe to a channel/room in order to receive the incoming events)?
Sample code to start listening to a room as some general user would be invaluable.
maybe a stupid question, but… will it scrobble to last.fm? it would be a cool idea
I feel this should be obvious, where’s the event for a track change?
Just use:
API.DJ_ADVANCE
It passes null when a DJ just switches to a new song/play.
It passes an object with a whole bunch of stuff if the DJ changes.
add shoutcast or someother way to dj live!
By the way, to get the chat ID is data.chatID (if anyone is wondering).
Perhaps there could be APIs for the different effects DJs can use, such as being able to turn lights off.
I also want a smartphone app for this! I would pay
How do use the api?
Cause no clear explation of a url of code can be found.|
No URLs are required. This is a JavaScript API. It’s available to your browser while you’re in the room.
For example, using Chrome, you can access the API by pressing F12 (or Ctrl+Shift+J) and entering commands directly into the JavaScript console.
It’d be great if the variables in objects like user, media and room would be shown on this documentation! : )
Here’s about the user object: https://github.com/TATDK/PCFW/wiki/UserObject
There will soon be more information on the wiki of PCFW about the other object types
Can i add inline function definitions for events? like:
API.addEventListener(API.USER_JOIN,function(data){API.sendChat(“Welcome ” + data.username);});
Yes, you don’t have to make a callback function, but simply do what you do.
There is one problem though with inline functions, you can’t remove them again with API.removeEventListener.
It would be awesome if you something like API.getHistory() would be added to return an array of maybe the past 30 songs or so
This’d be awesome. We could add a check so it skips the song if it’s been played in the last x minutes.
‘-’
There are some functionality to add a room as a widget in a page somewhere (say blogspot, or elsewhere), something like the tiny version for the pop up window of the chat. That would be great!
Thanxs
How exactly am I supposed to use the API?
Do have to include a specific library or something?
It’s JavaScript and you simply run it when in a room.
You can test them out in your browser’s console. (Chrome: Ctrl + Shift + J)
Is there any way I could use it from an external site, to make widget like:
Join our Plug.Dj Room Here
Currently Playing: Radioactive
Guests: 11
DJ’s: 2
You dont have to give me a code, just tell me if there is a library for it?
there is a way to do it yes. If you go to http://codingsoundtrack.org/ they have something similar to what you asked. But they are professional coders and programmers and know exactly what they are doing
A bit more documentation on the different objects
UserObject: https://github.com/TATDK/PCFW/wiki/UserObject
MediaObject: https://github.com/TATDK/PCFW/wiki/MediaObject
ScoreObject: https://github.com/TATDK/PCFW/wiki/ScoreObject
Is there a way to get the media that the dj’s will be playing? Having a playlist object with media objects in it would be really nice.
Why cant the getWaitList return the whole list? instead of only the ones on the screen?
For the chat event. It would be easy if you can request the rank of the person that sended the message. So If like a featured dj would talk i could use data.rank and it would return an int in this case 1. is this somthing you guys could make? Would make life a lot more easy
you can if they have mentioned you. just do something on the lines of API.getUser(data.fromID).permission. permissions go from 0 – 5 where 1 is normal user is 0 and host is 5… also for admins and ambassadors, you can set your own permissions for them. I use 50 for ambassadors and 100 for admins in my custom scripts
sorry, 0 is normal user, and 5 is host. 1 would be featured
Thank you, allready figured it out myself