Fork me on GitHub

Getting Started

With the Eve library for Node.js, one can create software agents in Javascript and run them in Node.js or in the browser.

This tutorial contains the following steps:

Prerequisites

To run Eve on Node.js, Node.js must be installed on your system. Node.js can be downloaded from the website http://nodejs.org/. There are installers available for Windows, Mac.

Install

Install the module via npm:

npm install evejs

Use

An agent basically has a methods send, receive, connect and disconnect. An agent can be extended with modules like pattern and request. There is a central configuration eve.system which can be used to load transports. The loaded transports can be used by agents to communicate with each other.

To set up a system with eve agents:

var eve = require('evejs');
  
  function MyAgent(id) {
    eve.Agent.call(this, id);
  
    // ...
  }
  
  MyAgent.prototype = Object.create(eve.Agent.prototype);
  MyAgent.prototype.constructor = MyAgent;
  
  MyAgent.prototype.receive = function (from, message) {
    // ...
  };
  
  module.exports = MyAgent;
eve.system.load({
    transports: [
      {
        type: 'distribus'
      }
    ]
  });
var agent1 = new MyAgent('agent1');
agent1.connect(eve.system.transports.getAll());
agent1.send('distribus://networkId/agent2', 'hello agent2!');
  agent1.send('agent2', 'hello agent2!'); // send via the default transport

The networkId of a transport can be found at transport.networkId.

HelloAgent

To create a simple agent class, create a file HelloAgent.js with the following code:

var eve = require('evejs');

function HelloAgent(id) {
  // execute super constructor
  eve.Agent.call(this, id);

  // connect to all transports configured by the system
  this.connect(eve.system.transports.getAll());
}

// extend the eve.Agent prototype
HelloAgent.prototype = Object.create(eve.Agent.prototype);
HelloAgent.prototype.constructor = HelloAgent;

HelloAgent.prototype.sayHello = function(to) {
  this.send(to, 'Hello ' + to + '!');
};

HelloAgent.prototype.receive = function(from, message) {
  console.log(from + ' said: ' + JSON.stringify(message));

  if (message.indexOf('Hello') === 0) {
    // reply to the greeting
    this.send(from, 'Hi ' + from + ', nice to meet you!');
  }
};

module.exports = HelloAgent;

This agent class can be used as follows. Note that the agents talk to each other via a LocalTransport which is instantiated in eve.system by default.

var HelloAgent = require('./HelloAgent');

// create two agents
var agent1 = new HelloAgent('agent1');
var agent2 = new HelloAgent('agent2');

// send a message to agent1
agent2.send('agent1', 'Hello agent1!');

Deployment

Node.js

The Node.js application can be deployed on any of the available hosting solutions for Node.js:

Browser

To use evejs in the browser, one can browserify the library and load it into any browser.

Site last rendered 30 March 2015