-
Notifications
You must be signed in to change notification settings - Fork 140
Usage
All functions have alternate forms, arguments are either passed:
- Through a single options object
- Individually
The options object has all the manually named arguments, passing arguments individually required them to be in the same order as they are shown in the documentation. Use .then to get results and to continue after the function is complete. To catch errors you follow up with a catch function which will receive any errors that occur during the function call, these often contain important information. Also remember that promises can be chained.
Example:
var rbx = require('noblox.js');
rbx.login('shedletsky', 'hunter2')
.then(function () {
console.log('Logged in')
})
.catch(function (err) {
console.error(err.stack);
});
or
var rbx = require('noblox.js');
var options = {
username: 'shedletsky',
password: 'hunter2'
}
rbx.login(options)
.then(function () {
console.log('Logged in')
})
.catch(function (err) {
console.error(err.stack);
});
EventEmitter are returned by some functions in place of promises. All emitters have three main events: the data
event, the error
event, and the close
event. The data
event is emitted whenever a piece of data is received. You should also bind the error
event, which emits if there is ever a request error during short polling (which is very possible when left on for a long time). If you do not bind an error
event your script will stop working if there is ever an error for some reason. Finally, the close
event is emitted if the event failed too many times: it stops the short poll and does not commit any more http requests. It can also be emitted if you want to end the stream yourself.
In this example the script collects one single new forum reply and then closes the event:
var onForumReply = rbx.onForumReply(forumId);
onForumReply.on('data', function (post) {
console.log(post);
onForumReply.emit('close');
});
onForumReply.on('error', function (err) {
console.error(err.stack);
});
There is a single global cookie jar stored in the module which will automatically be used if you don't specify a custom jar. If you only use the module with one user at a time this is the recommended method. The global cookie jar is stored in options. Example of retrieving it:
var rbx = require('noblox.js');
var jar = rbx.options.jar;
If you want to work with multiple users you can work directly with jar files. User session are stored in two ways: the default is simply an object containing the session and is structured like so:
var jar = {'session': 'AAAAA'};
If session_only
is disabled, jars are stored in a CookieJar
, which can be created with:
var request = require('request-promise');
var jar = request.jar();
CookieJar contains all cookies, including the session. For the most part these are just useless things that make requests larger, session only should be fine most of the time.
The easiest thing to do would be to simply get a jar file from the module, which will obey the session_only
rule and return a corresponding jar file:
var rbx = require('noblox.js');
var jar = rbx.jar();
Be aware that you must set something to refresh this token every once in a while: otherwise it will expire. Logging in every server restart and making a login interval of 1 day should be enough. The module does not check to make sure if you are logged in, you have to make sure of it yourself.
Many of the functions use simple caches in order to server requests faster. Cache time can be changed in settings.json. Cached items include XCSRF tokens and group roles: unless you change your group roles often the default cache settings should be fine. The cache works by saving request responses for a set amount of time (of course), but will refresh immediately if the item has expired. You may also set a time (or boolean) in which the item will serve an item based on the conditions above but silently refresh it if that has expired.
If you can you should change settings directly from the settings.json
file. You can also use rbx.settings
directly as shown below, remember to call rbx.options.init()
once you are done to update some things which are normally created off settings at startup.
var rbx = require('noblox.js');
var request = require('request');
rbx.settings.session_only = false;
rbx.settings.cache.XCSRF.expire = 60;
rbx.options.init();
More examples of usage are available on the noblox.js-server repository.
This wiki is currently outdated. It will be kept for reference purposes. It is highly recommended you visit https://noblox.js.org instead to see the latest documentation.