Introduction to ActionHero
ActionHero is a Node.js framework designed specifically for creating scalable and reusable APIs for both HTTP and WebSocket clients. With its exceptional capabilities, ActionHero allows developers to build powerful real-time servers quickly. Below, we explore dozens of useful ActionHero API examples along with code snippets to get you started.
Getting Started with ActionHero
To begin using ActionHero, you need to install it:
npm install actionhero
Creating Your First Action
Create a basic action file in your ActionHero project. Actions are the core building blocks of an ActionHero server:
// actions/hello.js
const { Action } = require('actionhero');
module.exports = class Hello extends Action {
constructor() {
super();
this.name = 'hello';
this.description = 'An ActionHero action';
this.outputExample = { message: 'Hello, actionhero!' };
}
async run({ response }) {
response.message = 'Hello, actionhero!';
}
};
Middleware Example
ActionHero allows you to create middleware to handle pre and post-processing:
// initializers/middleware.js
const { Initializer, api } = require('actionhero');
module.exports = class Middleware extends Initializer {
constructor() {
super();
this.name = 'middleware';
this.loadPriority = 1000;
}
async initialize() {
api.actions.addMiddleware({
name: 'logging',
global: true,
priority: 1000,
preProcessor: (data) => { api.log('Before action'); },
postProcessor: (data) => { api.log('After action'); }
});
}
};
Creating Chat Rooms
Enabling chat rooms with ActionHero is straightforward:
const { api } = require('actionhero');
api.chatRoom.addMiddleware({
name: 'roomLogger',
priority: 1000,
join: (connection, room) => {
api.log(`JOIN: ${connection.id} => ${room}`);
},
leave: (connection, room) => {
api.log(`LEAVE: ${connection.id} => ${room}`);
},
onSayReceive: (connection, room, messagePayload) => {
api.log(`RECEIVE: ${connection.id} @ ${room} => ${messagePayload.message}`);
}
});
Integrating Web Sockets
Enabling WebSocket communication in ActionHero is seamless:
const { api } = require('actionhero');
api.websocket.broadcast({}, { message: 'Hello to all!' });
Utilizing Task Scheduler
ActionHero features an excellent task scheduler for periodic jobs:
// tasks/example.js
const { Task } = require('actionhero');
module.exports = class ExampleTask extends Task {
constructor() {
super();
this.name = 'exampleTask';
this.description = 'An example task';
this.frequency = 1000 * 60; // Runs every minute
this.queue = 'default';
}
async run() {
api.log('task running...');
}
};
App Example with ActionHero APIs
Now, let’s build a sample application to see all these APIs in action.
// index.js
const { Process, api } = require('actionhero');
class Main extends Process {
async start() {
await super.start();
api.log('Process Started');
}
}
async function startServer() {
const server = new Main();
await server.start();
}
startServer();
With these code snippets and explanations, you should be well on your way to mastering ActionHero.
Conclusion
ActionHero is a powerful framework for building scalable and reusable APIs, making it an excellent choice for real-time server applications. By exploring the examples provided, you can gain a solid understanding of its capabilities and start integrating ActionHero into your own projects.
Hash: a96979864c5c94dc2e3037e686cfa8e09523f727406b694e6e068474c95af402