npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

actionhero-node-client

v8.0.1

Published

ActionHero client for other node.js servers to use

Downloads

43

Readme

ActionheroNodeClient

For one node.js servers talking to another ActionHero server, over the socket protocol.

NPM Version Node Version Greenkeeper badge Build Status

This library makes it easy for one nodeJS process to talk to a remote actionhero server.

This library makes use of actionhero's TCP socket connections to enable fast, stateful connections. This library also allows for many concurrent and asynchronous requests to be running in parallel by making use of actionhero's message counter.

notes:

  • This Library is a server-server communication library, and is NOT the same as the websocket client library that is generated via the actionhero server.
  • Node.js v8+ is required to use this package, as it uses async/await.

Setup

Installation should be as simple as:

npm install --save actionhero-node-client

and then you can include it in your projects with:

var ActionheroNodeClient = require("actionhero-node-client");
var client = new ActionheroNodeClient();

Once you have included the ActionheroClient library within your project, you can connect like this:

await client.connect({
  host: "127.0.0.1",
  port: "5000",
});

default options (which you can override) are:

var defaults = {
  host: "127.0.0.1",
  port: "5000",
  delimiter: "\r\n",
  logLength: 100,
  secure: false,
  timeout: 5000,
  reconnectTimeout: 1000,
  reconnectAttempts: 10,
};

Methods

One you are connected (by waiting for the "connected" event or using the connect callback), the following methods will be available to you:

  • await ActionheroNodeClient.connect()
  • await ActionheroNodeClient.disconnect()
  • {error, data, delta} = await ActionheroNodeClient.paramAdd(key, value)
    • remember that both key and value must pass JSON.stringify
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.paramDelete(key)
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.paramsDelete()
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.paramView(key)
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.paramsView()
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.details()
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.roomView(room)
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.roomAdd(room)
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.roomLeave(room)
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • {error, data, delta} = await ActionheroNodeClient.say(room, msg)
    • msg can be a string or an Object
  • {error, data, delta} = await ActionheroNodeClient.action(action)
    • this action method will not set or unset any params, and use those already set by paramAdd
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)
  • ActionheroNodeClient.actionWithParams(action, param)
    • this action will ignore any previously set params to the connection
    • params is a hash of this form {key: "myKey", value: "myValue"}
    • The return value will contain the response from the server (data), a possible error (error), and the response's duration (delta)

Events

ActionheroNodeClient will emit a few types of events (many of which are caught in the example below). Here are the events, and how you might catch them:

  • client.on("connected")
  • client.on("end")
  • client.on("welcome", (welcomeMessage) => {})
    • welcomeMessage is a string
  • client.on("error", (error) => {})
    • errorMessage is a string
    • This is only emitted for connection errors, not errors to your requests/actions
  • client.on("say", (message) => {})
    • message is a hash containing timeStamp, room, from, and message
  • client.on("timeout", (error, request, caller) => {})
    • request is the string sent to the api
    • caller (the calling function) is also returned to with an error

Data

There are a few data elements you can inspect on actionheroClient:

  • ActionheroNodeClient.lastLine
    • This is the last parsed JSON message received from the server (chronologically, not by messageID)
  • ActionheroNodeClient.userMessages
    • a hash which contains the latest say message from all users
  • ActionheroNodeClient.log
    • An array of the last n parsable JSON replies from the server
    • each entry is of the form {data, timeStamp} where data was the server's full response
  • ActionheroNodeClient.messageCount
    • An integer counting the number of messages received from the server

Example

var ActionheroNodeClient = require("actionhero-node-client");

async function main () {
  const client = new ActionheroNodeClient()

  client.on('say', (message) => {
    console.log(' > SAY: ' + message.message + ' | from: ' + message.from)
  })

  client.on('welcome', (welcome) => {
    console.log('WELCOME: ' + welcome)
  })

  client.on('error', (error) => {
    console.log('ERROR: ' + error)
  })

  client.on('end', () => {
    console.log('Connection Ended')
  })

  client.on('timeout', (request, caller) => {
    console.log(request + ' timed out')
  })

  await client.connect({host: '127.0.0.1', port: '5000'})

  // get details about myself
  console.log('My Details: ', client.details)

  // try an action
  const params = { key: 'mykey', value: 'myValue' }
  let {error, data, delta} = await client.actionWithParams('cacheTest', params)
  if (error) { throw error }
  console.log('cacheTest action response: ', data)
  console.log(' ~ request duration: ', delta)

  // join a chat room and talk
  await client.roomAdd('defaultRoom')
  await client.say('defaultRoom', 'Hello from the actionheroClient')
  await client.roomLeave('defaultRoom')

  // leave
  await client.disconnect()
  console.log('all done!')
}

main()