Node.js client for agar.io with API.
- Install Node.js
- Install client using
npm install agario-client
(ignorepython
errors) - Run
node ./node_modules/agario-client/examples/basic.js
(for testing purpose) - If it works, you're ready to look at API and code
There are two types of object that have API:
- Client - thing that connects to agar.io server and talks to it. If you want to spawn and control your
Ball
, you need to talk withClient
- Ball - thing that
Client
creates. Everything in game isBalls
(viruses/food/players...). You can't controlBalls
objects, only observe what they do.
Both objects have same methods for events from events.EventEmitter:
.on('eventName', callback)
attach listener to event.once('eventName', callback)
attach listener to event but execute only once.removeListener('eventName', callback)
remove listener from event.removeAllListeners('eventName')
remove all listeners from event.emit('eventName', p1, p2...)
emit your own event- check more in documentation
var AgarioClient = require('agario-client');
var client = new AgarioClient(client_name);
client_name is string for client that will be used for logging (if you enable it). It's not your ball name.
Properties that you can change:
client.debug
debug level. 0-5. 0 is completely silent. 5 is super verbose. Default: 1client.server
address that was used inclient.connect()
callclient.key
key that was used inclient.connect()
callclient.facebook_key
key to login. See how to get key in additional info.client.agent
agent to use for connection. Check additional info.client.inactive_destroy
time in ms for how long ball will live in memory after his last known action (if player exit from game or ball eaten outside our field of view, we will not know it since server sends action only about field that you see. Original codedestroy()
Ball
when hedisappear
from field of view. You can do that inclient.on('ballDisppear')
if you want it for some reason). Default: 5*60*1000 (5 minutes)client.inactive_check
time in ms for time interval that search and destroy inactiveBalls
. Default: 10*1000 (10 seconds)client.spawn_attempts
how much we need try spawn before disconnect (made for unstable spawn on official servers). Default: 25client.spawn_interval
time in ms between spawn attempts. Default: 200
Properties that better not to change or you can break something:
client.balls
object with allBalls
thatclient
knows about. AccessBall
likeclient.balls[ball_id]
client.my_balls
array of aliveBall
's IDs thatclient
owns and can control.client.score
personal score since spawnclient.leaders
array of leader'sBalls
IDs in FFA mode. (player can have lots ofBalls
, but sever sends only one ID of oneBall
)client.teams_scores
array of team's scores in teams modeclient.client_name
name that you have set forclient
(not nickname)client.tick_counter
number of tick packets received (i call them ticks because they contains information about eating/movement/size/disappear... ofBalls
)
client.connect(server, key)
connect to agar.io server. Check Servers part in this readme to see how to get server IP and key. ProTip: each server have few rooms (if it is not party), so you may need connect few times before you will get in room that you want (but you need new key each time and agar.io can ban your IP for flooding with requests). You can lookclient.once('leaderBoardUpdate')
to know if you're connected to correct roomclient.disconnect()
disconnect from serverclient.spawn(name)
will spawnBall
with nickname.client.on('myNewBall')
will be called when server sends ourBall
info. Will returnfalse
if connection is not established.client.spectate()
will activate spectating mode. Look atclient.on('spectateFieldUpdate')
for FOV updates. Will returnfalse
if connection is not established.client.spectateModeToggle()
switching spectate mode in spectating mode (Q key in official client). Useclient.moveTo()
to move your "camera" around. Look atclient.on('spectateFieldUpdate')
for movement updates. Will returnfalse
if connection is not established.client.moveTo(x,y)
send move command.x
andy
is numbers where you want to move. Coordinates (size) of map you can get inclient.on('mapSizeLoad')
. YourBalls
will move to coordinates you specified until you send new coordinates to move. Original source code do this every 100ms (10 times in second) and before split and eject. Will returnfalse
if connection is not established.client.split()
will split yourBalls
in two.Ball
will be ejected in last direction that you sent withclient.moveTo()
.client.on('myNewBall')
will be called when server sends ourBalls
info. Will returnfalse
if connection is not established.client.eject()
will eject some mass from yourBalls
. Mass will be ejected in last direction that you sent withclient.moveTo()
. Ejected mass isBall
too (but we don't own them). Soclient.on('ballAppear')
will be called when server sends ejected massBalls
info. Will returnfalse
if connection is not established.
In this list on.eventName(param1, param2)
means you need to do client.on('eventName', function(param1, param2) { ... })
on.connecting()
connecting to serveron.connected()
connected to server and ready to spawnon.connectionError(err)
connection erroron.disconnect()
disconnectedon.message(packet)
new packet received from server (checkpacket.js
)on.myNewBall(ball_id)
my newBall
created (spawn/split/explode...)on.somebodyAteSomething(eater_id, eaten_id)
somebody ate somethingon.scoreUpdate(old_score, new_score)
personal score updatedon.leaderBoardUpdate(old_array, new_array)
leaders update in FFA mode. Array of leader'sBall
's IDs (one ID per leader)on.teamsScoresUpdate(old_scores, new_scores)
array of teams scores update in teams modeon.mapSizeLoad(min_x, min_y, max_x, max_y)
map size update (after connect)on.reset()
when we delete allBalls
and stop timers (connection lost?)on.winner(ball_id)
somebody won and server going for restarton.ballAction(ball_id, coordinate_x, coordinate_y, size, is_virus, nick)
some action about someBall
on.ballAppear(ball_id)
Ball
appear on "screen" (field of view)on.ballDisppear(ball_id)
Ball
disappear from "screen" (field of view)on.ballDestroy(ball_id, reason)
Ball
deleted (check reasons at the bottom of this document)on.mineBallDestroy(ball_id, reason)
mine (your)Ball
deleted (check reasons at the bottom of this document)on.lostMyBalls()
all mineBalls
destroyed/eatenon.merge(destroyed_ball_id)
mine twoBalls
connects into oneon.ballMove(ball_id, old_x, old_y, new_x, new_y)
Ball
moveon.ballResize(ball_id, old_size, new_size)
Ball
resizeon.ballRename(ball_id, old_name, new_name)
Ball
set name/change name/we discover nameon.ballUpdate(ball_id, old_update_time, new_update_time)
new data about ball receivedon.spectateFieldUpdate(cord_x, cord_y, zoom_level)
coordinates of field of view inclient.spectate()
modeon.experienceUpdate(level, current_exp, need_exp)
experience information update (if logined with facebook key)on.debugLine(line_x, line_y)
the server sometimes sends a line for the client to render from your ball to the point though don't expect to see it.
var ball = client.balls[ball_id];
ball_id is number that you can get from events
Properties that you can change:
- None. But you can create properties that don't exists for your needs if you want
Properties that better not to change or you can break something:
ball.id
ID ofBall
(number)ball.name
nickname of player that own theBall
ball.x
last known X coordinate ofBall
(ifball.visible
istrue
then its current coordinate)ball.y
last known Y coordinate ofBall
(ifball.visible
istrue
then its current coordinate)ball.size
last known size ofBall
(ifball.visible
istrue
then its current size)ball.mass
mass of ball calculated fromball.size
ball.color
string with color ofBall
ball.virus
iftrue
then ball is a virus (green thing that explode big balls)ball.mine
iftrue
then we do own thisBall
ball.client
Client
that knows thisBall
(if notball.destroyed
)ball.destroyed
iftrue
then thisBall
no more exists, forget about itball.visible
iftrue
then we see thisBall
on our "screen" (field of view)ball.last_update
timestamp when we last saw thisBall
ball.update_tick
last tick when we saw thisBall
ball.toString()
will returnball.id
and(ball.name)
if set. So you can logball
directly- Other methods is for internal use
In this list on.eventName(param1, param2)
means you need to do ball.on('eventName', function(param1, param2) { ... })
on.destroy(reason)
Ball
deleted (check reasons at the bottom of this document)on.move(old_x, old_y, new_x, new_y)
Ball
moveon.resize(old_size, new_size)
Ball
resizeon.update(old_time, new_time)
new data aboutBall
receivedon.rename(old_name, new_name)
Ball
change/set name/we discover nameon.appear()
Ball
appear on "screen" (field of view)on.disappear()
Ball
disappear from "screen" (field of view)
When you do var AgarioClient = require('agario-client');
you can access AgarioClient.servers
Functions need opt
as options object and cb
as callback function.
All functions can accept:
opt.agent
to use for connection. Check additional info
opt.resolve
set to true
to resolve IP on client side (since SOCKS4 can't accept domain names)
opt.ip
if you resolved m.agar.ip
IP by other way (will cancel opt.resolve
).
servers.getFFAServer(opt, cb)
to request FFA server.
Needsopt.region
servers.getTeamsServer(opt, cb)
to request teams server.
Needsopt.region
servers.getExperimentalServer(opt, cb)
to request experimental server.
Needsopt.region
Use at your own risk! Support of experimental servers are not guaranteed by agario-client!servers.getPartyServer(opt, cb)
to request party server.
Needsopt.party_key
servers.createParty(opt, cb)
to request party server.
Needsopt.region
Check region list below in this file.
Callback will be called with single object that can contain:
server
- server's IP:PORT (addws://
before passing to connect())key
- server's keyerror
- error code (WRONG_HTTP_CODE
/WRONG_DATA_FORMAT
/REQUEST_ERROR
/LOOKUP_FAIL
)error_source
- error object passed fromreq.on.error
when available (for example whenREQUEST_ERROR
happens)res
- response object when available (for example whenWRONG_HTTP_CODE
happens)data
- response data string when available (for example whenWRONG_DATA_FORMAT
happens)
LOOKUP_FAIL
can happen only if opt.lookup
was set to true
and will have only error_source
You can check how examples/basic.js
uses this.
If you want record/repeat or watch in real time what your client doing through web browser, you might want to check agario-devtools
- BR-Brazil
- CN-China
- EU-London
- JP-Tokyo
- RU-Russia
- SG-Singapore
- TK-Turkey
- US-Atlanta
{'reason': 'reset'}
whenclient
destroys everything (connection lost?){'reason': 'inactive'}
when we didn't sawBall
forclient.inactive_destroy
ms{'reason': 'eaten', 'by': ball_id}
whenBall
got eaten{'reason': 'merge'}
when ourBall
merges with our otherBall
Facebook key currently stored in localStorage
of browser.
First it was in JSON.parse(localStorage.loginCache).ga
Then in JSON.parse(localStorage.loginCache).ha
Last known JSON.parse(localStorage.loginCache3).authToken
To get key, you need
- Open http://agar.io
- Log-in through facebook
- Open browser's JS console
- Paste
JSON.parse(localStorage.loginCache3).authToken
- Press
Enter
key on keyboard
If there is no key, then its location may be changed again. Create an issue or email me.
You can change default agent for AgarioClient
and AgarioClient.servers
to use for connections. You can use libs to do it. For testing and example i used socks lib. Execute node ./node_modules/agario-client/examples/socks.js
to test it and read examples/socks.js
file to see how to use SOCKS. For proxy you will need to use some other lib.
You can add your own properties/events to clients/balls.
var AgarioClient = require('agario-client');
- Prototype of
Client
is located atAgarioClient.prototype.
- Prototype of
Ball
is located atAgarioClient.Ball.prototype.
For example:
AgarioClient.Ball.prototype.isMyFriend = function() { ... }; //to call ball.isMyFriend()
AgarioClient.prototype.addFriend = function(ball_id) { ... }; //to call client.addFriend(1234)
Events:
client.on('somebodyAteSomething', function(eater_id, eaten_id) { #eat event
if(client.balls[eaten_id].isMyFriend()) { //if this is my friend
client.emit('friendEaten', eater_id, eaten_id); //emit custom event
}
});
client.on('friendEaten', function(eater_id, friend_id) { //on friend eaten
client.log('My friend got eaten!');
});
Check full example in examples/basic.js
If something is broken, please email me or create issue. I will not know that something is broken until somebody will tell me that.
MIT