Skip to content
tristanls edited this page Nov 24, 2012 · 8 revisions

The canonical Hello World example can be found in howto/hello-world

crosstalk.on( 'hello', function ( params, callback ) {
  if ( callback ) callback( null, { hello : "world" } );
});

Upon completion of this HOWTO you will accomplish all of the following:

  • Create a new Crosstalk worker project
  • Deploy a Crosstalk worker to Crosstalk Swarm
  • List workers deployed on Crosstalk Swarm
  • Start a worker on Crosstalk Swarm
  • List running workers on Crosstalk Swarm
  • Use Crosstalk IDE to send a message to a worker running on Crosstalk Swarm
  • Stop a worker running on Crosstalk Swarm

Create hello-world project

Starting in a directory where you want your worker projects to reside:

~$ mkdir hello-world
~$ cd hello-world

Create hello.js worker file

Create a file hello-world/hello.js with the following contents:

crosstalk.on( 'hello', function ( params, callback ) {
  if ( callback ) callback( null, { hello : "world" } );
});

At this point, your project directory structure should look like:

hello-world
|-- hello.js

Create package.json package file

Create a file hello-world/package.json with the following contents:

{
  "name": "your-name-hello-world",
  "version": "0.0.0",
  "description": "Crosstalk 'hello world' worker",
  "main": "hello.js",
  "author": "Your Name <your.name@email.com>",
  "crosstalk": {
    "type": "worker"
  },
  "private": true
}

your-name-hello-world should be replaced with your account name in place of your-name. The global Crosstalk worker namespace is flat. So, by convention, your account name should be included in front of your worker name. For example, if your account name is bob, then your hello-world worker would be called bob-hello-world.

The field "crosstalk": {"type": "worker"} is required. It identifies the project as a Crosstalk worker project.

At this point, your project directory structure should look like:

hello-world
|-- hello.js
|-- package.json

Create config.json configuration file

Create a file hello-world/config.json with the following contents:

{}

Our hello-world worker does not use any configuration. The contents of this file are required when starting a worker later on. The contents are accessible from within the worker via:

var config = require( 'config' );

At this point, your project directory structure should look like:

hello-world
|-- config.json
|-- hello.js
|-- package.json

Upload the worker to Crosstalk Swarm

Assumptions:

  1. crosstalk-cli is available in directory $CROSSTALK_CLI_HOME
  2. Your workers directory is $CROSSTALK_WORKERS

Run the following command:

~$ $CROSSTALK_CLI_HOME/bin/crosstalk worker create

You will receive a prompt:

prompt: Path to Crosstalk worker directory:

In the prompt enter the path to your worker:

prompt: Path to Crosstalk worker directory: $CROSSTALK_WORKERS/hello-world

Your worker will be created:

info:    Creating worker your-name-hello-world@0.0.0
info:    Worker your-name-hello-world@0.0.0 created.
info:    Crosstalk OK

List your workers.

Run the following command:

~$ $CROSSTALK_CLI_HOME/bin/crosstalk worker list

You will see a list of your created workers:

info:    Retrieving uploaded worker list
data:    workers(1):
data:     your-name-hello-world@0.0.0
info:    Crosstalk OK

Start your worker.

Run the following command:

~$ $CROSSTALK_CLI_HOME/bin/crosstalk worker start your-name-hello-world@0.0.0

You will receive prompt for your configuration file:

prompt: Path to worker configuration JSON file: 

Enter path to your configuration file:

prompt: Path to worker configuration JSON file: $CROSSTALK_WORKERS/hello-world/config.json

Your worker will be started:

info:    Starting worker your-name-hello-world@0.0.0
info:    Worker your-name-hello-world@0.0.0 started as crosstalk-drone-worker-7ee654e1-a009-4166-8402-8660be371d99
info:    Crosstalk OK

List your running workers

Run the following command:

~$ $CROSSTALK_CLI_HOME/bin/crosstalk worker running

You will see a list of your running workers:

info:    Retrieving running worker list
data:    workers(1):
data:     your-name-hello-world@0.0.0: crosstalk-drone-worker-7ee654e1-a009-4166-8402-8660be371d99
info:    Crosstalk OK

Send a message to your running worker

Create a file hello-world/test/manual.js with the following contents:

var ide = require( 'crosstalk-ide' )();

ide.send(
  "<YOUR CROSSTALK TOKEN>",
  "hello",
  {},
  null,
  function ( error, response ) {
    console.log( error, response );
  }
);

You will find <YOUR CROSSTALK TOKEN> in $CROSSTALK_CLI_HOME/config/config.json. Copy and paste it from there.

Additionally, you will need to npm link crosstalk-ide in order to make it available for your manual.js test. To link, navigate to your $CROSSTALK_WORKERS/hello-world and execute the following command:

$CROSSTALK_WORKERS/hello-world$ npm link crosstalk-ide

Now you should be ready to use crosstalk-ide.

At this point, your project directory structure should look like:

hello-world
|-- test
    |-- manual.js
|-- config.json
|-- hello.js
|-- package.json

Run the manual test:

~/hello-world$ node test/manual.js

You should see the following in the console:

{"message":"SENDING TO CROSSTALK","data":{"message":"hello","params":{},"scope":null},"level":"info","timestamp":"2012-11-24T22:41:12.698Z"}
{"message":"STATUS CODE","data":{"statusCode":"200"},"level":"info","timestamp":"2012-11-24T22:41:15.474Z"}
{"message":"RESPONSE","data":{"message":"hello","response":{"hello":"world"}},"level":"info","timestamp":"2012-11-24T22:41:15.474Z"}
null { hello: 'world' }

Your worker is up and running! (The first three JSON lines are debug statements from crosstalk-ide. The last line is the output from the callback you specified in manual.js: console.log( error, response );)

Stop your worker

Run the following command (replace crosstalk-drone-worker-... with the one for your worker):

~$ $CROSSTALK_CLI_HOME/bin/crosstalk worker stop crosstalk-drone-worker-7ee654e1-a009-4166-8402-8660be371d99

You will see confirmation that your worker was stopped:

info:    Stopping worker crosstalk-drone-worker-7ee654e1-a009-4166-8402-8660be371d99
info:    Worker crosstalk-drone-worker-7ee654e1-a009-4166-8402-8660be371d99 is stopping.
info:    Crosstalk OK

Your worker is now stopped.

You now know how to:

  • Create a new Crosstalk worker project
  • Deploy a Crosstalk worker to Crosstalk Swarm
  • List workers deployed on Crosstalk Swarm
  • Start a worker on Crosstalk Swarm
  • List running workers on Crosstalk Swarm
  • Use Crosstalk IDE to send a message to a worker running on Crosstalk Swarm
  • Stop a worker running on Crosstalk Swarm