-
Notifications
You must be signed in to change notification settings - Fork 5
Websocket API
Socket tokens are like session tokens, except they can only be used to authenticate a socket connection. They also expire quickly, and must be used within 5 minutes of obtaining it.
GET /api/socket
Example Request:
curl -X GET \
-H "Authorization: bearer <token>" \
https://nitro-server.herokuapp.com/api/socket
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"socketToken": "<token>"
}
Connections are given three seconds to send the 'user.auth' message before they are disconnected.
jandal = new Jandal()
sock = new SockJS("https://nitro-server.herokuapp.com/socket");
jandal.connect(sock);
jandal.on('socket.open', function () {
console.log('we are online');
jandal.emit('user.auth', socketToken, function (err) {
if (err) return console.log('error authorizing");
console.log('successfully authorized socket');
});
});
If authorization is successful, err
will be null and status
will be true.
If authorization fails, err
will contain the error message and status
will be null.
jandal.emit('user.auth', socketToken, function (err, status) {
if (err) {
console.log('could not authorize connection - try another socketToken');
} else {
console.log('connection is now authorized');
}
});
Returns user information
Parameters:
-
callback
: returns the user information.
Example Request:
jandal.emit('user.info', function (err, info) {
console.log(info);
});
Example Response:
[null, {
"id": 2,
"name": "sam",
"email": "crazycheese@gmail.com",
"pro": false,
"createdAt": "2014-01-04T29:58:34.448Z"
}]
Get all the tasks.
Parameters:
-
callback
: returns the tasks
Example Request:
jandal.emit('task.read', function (err, tasks) {
console.log(tasks);
});
Example Response:
[null, [
{
"id": 32,
"userId": 2,
"listId": 12,
"name": "Fix all the bugs",
"notes": "",
"date": 1392781001,
"priority": 2,
"completed": 0
},
{ ... }
]]
Create a task. Returns the task id.
Parameters:
-
data
: the task data. Should not containid
oruserId
. -
callback
: returns the task id.
Example Request:
var task = {
listId: 30,
name: 'my task',
notes: '',
date: 0,
priority: 1,
completed: 0
};
jandal.emit('task.create', task, function (err, task) {
console.log('created task with id', task.id);
});
Example Response:
[null, {
"id": 21,
"userId": 2,
"listId": 30,
"name": "my task",
"notes": "",
"date": 0,
"priority:" 1,
"completed": 0
}]
Update an existing task.
Parameters:
-
id
: the task id -
data
: the changes to make to the task -
[callback]
: optional callback. Returns the updated task.
data
can contain any task property, except id
, listId
or userId
.
If you want to change the list a task is in, use task.move
.
Example Request:
var id = 42;
var changes = {
name: 'I changed the name of the task'
};
jandal.emit('task.update', id, changes, function (err, task) {
console.log('full task', task);
});
Example Response:
[null, {
"id": 42,
"userId": 2,
"listId": 30,
"name": "I changed the name of the task",
"notes": "",
"date": 0,
"priority": 1,
"completed": 0
}]
Move a task to another list.
Parameters:
-
taskId
: the task id -
listId
: the list id -
[callback]
: an optional callback. Returns the updated task.
Example Request:
var taskId = 21;
var listId = 42;
jandal.emit('task.move', taskId, listId, function (err, task) {
console.log(task);
});
Example Response:
[null, {
"id": 21,
"userId": 2,
"listId": 42,
"name": "the task name",
"notes": "",
"date": 0,
"priority": 1,
"completed": 0
}]
Destroy a task.
Parameters:
-
id
: the task id -
[callback]
: an optional callback. Return true.
Example Request:
jandal.emit('task.destroy', 42, function (err, success) {
console.log('destroyed the task');
});
Example Response:
[null, true]
Get all the lists.
Parameters:
-
[callback]
: returns the lists
Example Request:
jandal.emit('list.read', function (err, lists) {
console.log(lists);
});
Example Response:
[null, [
{
"id": 20,
"userId": 2,
"name": "Inbox"
},
{ ... }
]]
Create a list.
Parameters:
-
data
: the list data. Should not includeid
oruserId
. -
[callback]
: returns the list
Example Request:
var list = {
name: "Shopping List"
};
jandal.emit('list.create', list, function (err, id) {
console.log('created a new list with the id', id);
});
Example Response:
[null, {
"id": 21,
"userId": 2,
"name": "Shopping List"
}]
Update an existing list.
Parameters:
-
id
: the list id -
data
: the changes to make to the list -
[callback]
: optional callback
data
can contain any list property except id
or listId
.
Example Request:
var id = 21;
var changes = {
name: 'Renamed the list'
};
jandal.emit('list.update', id, changes, function (err, list) {
console.log(changes);
});
Example Response:
[null, {
"id": 21,
"userId": 2,
"name": "Renamed the list"
}]
Destroy an existing list.
jandal.emit('list.destroy', id, function (err, success) {
console.log('deleted list');
});
Example Response:
[null, true]
Format:
{
"sort": unsigned int,
"night": unsigned int,
"language": string - length 5,
"weekStart": unsigned int,
"dateFormat": string - length 7,
"moveCompleted": unsigned int,
"confirmDelete": unsigned int
}
Update a preference. You can change any setting.
var changes = {
"night": 0,
"confirmDelete": 1
};
jandal.emit('pref.update', changes, function (err, prefs) {
console.log(prefs);
});
Example Response:
[null, {
"sort": 1,
"night": 0,
"language": "en-us",
"weekStart": 1,
"dateFormat": "dd/mm/yy",
"moveCompleted": 0,
"confirmDelete": 1
}]
Send an array of events. This is useful for handling changes made while the client is offline.
clientTime
is the time on the client in seconds. It is used to account for
timezone differences between the server and the client.
clientTime = Math.floor(Date.now() / 1000)
A callback
is required. The server returns an export of the all the tasks,
lists and prefs owned by the user.
jandal.emit('queue.sync', queue, clientTime, function (err, data) {
console.log('tasks', data.task);
console.log('lists', data.list);
console.log('prefs', data.pref);
});
Example Response:
[null, {
"list": [
],
"task": [
],
"pref": {
}
}]