Skip to content

Commit 56a271c

Browse files
feat: add hono adapter (#670)
* feat: add hono adapter * Update README.md
1 parent 92b6edc commit 56a271c

File tree

11 files changed

+773
-0
lines changed

11 files changed

+773
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ With this library you get a beautiful UI for visualizing what's happening with e
2727
| [@bull-board/koa](https://www.npmjs.com/package/@bull-board/koa) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/koa) |
2828
| [@bull-board/hapi](https://www.npmjs.com/package/@bull-board/hapi) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/hapi) |
2929
| [@bull-board/nestjs](https://www.npmjs.com/package/@bull-board/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/nestjs) |
30+
| [@bull-board/hono](https://www.npmjs.com/package/@bull-board/hono) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/hono) |
3031

3132
## Notes
3233

@@ -51,6 +52,10 @@ yarn add @bull-board/fastify
5152
yarn add @bull-board/hapi
5253
# or
5354
yarn add @bull-board/koa
55+
# or
56+
yarn add @bull-board/nestjs
57+
# or
58+
yarn add @bull-board/hono
5459
```
5560

5661
### NestJS specific setup
@@ -105,6 +110,7 @@ For more advanced usages check the `examples` folder, currently it contains:
105110
5. [With Koa.js server](https://github.com/felixmosh/bull-board/tree/master/examples/with-koa)
106111
6. [With Nest.js server using the built-in module](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs-module) (Thanx to @dennissnijder)
107112
7. [With Nest.js server using the express adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs) (Thanx to @lodi-g)
113+
8. [With Hono server](https://github.com/felixmosh/bull-board/tree/master/examples/with-hono) (Thanks to @nihalgonsalves)
108114

109115
### Board options
110116
1. `uiConfig.boardTitle` (default: `Bull Dashboard`)

examples/with-hono/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Hono example
2+
3+
This example shows how to use [Hono](https://hono.dev) as a server for bull-board.

examples/with-hono/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const { createBullBoard } = require('@bull-board/api');
2+
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');
3+
const { HonoAdapter } = require('@bull-board/hono');
4+
const { Queue: QueueMQ, Worker } = require('bullmq');
5+
const { Hono } = require('hono');
6+
const { showRoutes } = require('hono/dev');
7+
const { serve } = require('@hono/node-server');
8+
const { serveStatic } = require('@hono/node-server/serve-static');
9+
10+
const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t * 1000));
11+
12+
const redisOptions = {
13+
port: 6379,
14+
host: 'localhost',
15+
password: '',
16+
tls: false,
17+
};
18+
19+
const createQueueMQ = (name) => new QueueMQ(name, { connection: redisOptions });
20+
21+
async function setupBullMQProcessor(queueName) {
22+
new Worker(
23+
queueName,
24+
async (job) => {
25+
for (let i = 0; i <= 100; i++) {
26+
await sleep(Math.random());
27+
await job.updateProgress(i);
28+
await job.log(`Processing job at interval ${i}`);
29+
30+
if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`);
31+
}
32+
33+
return { jobId: `This is the return value of job (${job.id})` };
34+
},
35+
{ connection: redisOptions }
36+
);
37+
}
38+
39+
const run = async () => {
40+
const exampleBullMq = createQueueMQ('BullMQ');
41+
42+
await setupBullMQProcessor(exampleBullMq.name);
43+
44+
const app = new Hono();
45+
46+
const serverAdapter = new HonoAdapter(serveStatic);
47+
48+
createBullBoard({
49+
queues: [new BullMQAdapter(exampleBullMq)],
50+
serverAdapter,
51+
});
52+
53+
const basePath = '/ui'
54+
serverAdapter.setBasePath(basePath);
55+
app.route(basePath, serverAdapter.registerPlugin());
56+
57+
app.get('/add', async (c) => {
58+
await exampleBullMq.add('Add', { title: c.req.query('title') });
59+
60+
return c.json({ ok: true })
61+
});
62+
63+
showRoutes(app);
64+
65+
serve({ fetch: app.fetch, port: 3000 }, ({ address, port }) => {
66+
/* eslint-disable no-console */
67+
console.log(`Running on ${address}:${port}...`);
68+
console.log(`For the UI of instance1, open http://localhost:${port}/ui`);
69+
console.log('Make sure Redis is running on port 6379 by default');
70+
console.log('To populate the queue, run:');
71+
console.log(` curl http://localhost:${port}/add?title=Example`);
72+
/* eslint-enable */
73+
})
74+
};
75+
76+
run().catch((e) => {
77+
console.error(e);
78+
process.exit(1);
79+
});

examples/with-hono/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bull-board-with-hono",
3+
"version": "1.0.0",
4+
"description": "Example of how to use Hono server with bull-board",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "felixmosh",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@bull-board/hono": "../../packages/hono",
14+
"@hono/node-server": "^1.4.0",
15+
"bullmq": "^4.6.0",
16+
"hono": "^3.12.0"
17+
}
18+
}

0 commit comments

Comments
 (0)