{"id":25751931,"url":"https://github.com/0xomaradel/fastify-rabbitmq","last_synced_at":"2026-05-13T18:37:49.913Z","repository":{"id":279564756,"uuid":"939222309","full_name":"0xOmarAdel/fastify-rabbitmq","owner":"0xOmarAdel","description":"A Fastify and RabbitMQ demo where a producer publishes fake user and country data, and two consumers receive messages based on routing keys. This setup illustrates simple distributed messaging across multiple Fastify servers.","archived":false,"fork":false,"pushed_at":"2025-02-26T07:56:35.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-26T08:35:42.296Z","etag":null,"topics":["fastify","fastify-plugin","rabbitmq","rabbitmq-consumer","rabbitmq-producer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0xOmarAdel.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-26T07:38:07.000Z","updated_at":"2025-02-26T08:13:08.000Z","dependencies_parsed_at":"2025-02-26T08:35:50.921Z","dependency_job_id":"d8efabaf-0e8e-4ed8-b0b2-1d1686d20578","html_url":"https://github.com/0xOmarAdel/fastify-rabbitmq","commit_stats":null,"previous_names":["0xomaradel/fastify-rabbitmq"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xOmarAdel%2Ffastify-rabbitmq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xOmarAdel%2Ffastify-rabbitmq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xOmarAdel%2Ffastify-rabbitmq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xOmarAdel%2Ffastify-rabbitmq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xOmarAdel","download_url":"https://codeload.github.com/0xOmarAdel/fastify-rabbitmq/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240867432,"owners_count":19870405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin","rabbitmq","rabbitmq-consumer","rabbitmq-producer"],"created_at":"2025-02-26T14:18:43.617Z","updated_at":"2026-05-13T18:37:47.948Z","avatar_url":"https://github.com/0xOmarAdel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fastify RabbitMQ Multi‑Server Project\n\nThis project demonstrates a simple distributed messaging setup using Fastify and RabbitMQ. It consists of three separate servers:\n\n- **Producer (Port 3000):** Publishes fake user and country data to a RabbitMQ exchange.\n- **Consumer‑1 (Port 3001):** Consumes only user messages.\n- **Consumer‑2 (Port 3002):** Consumes both user and country messages.\n\nAll servers use the following environment variables:\n\n```env\nRABBITMQ_CONNECTION=amqp://guest:guest@localhost\nRABBITMQ_EXCHANGE=ex.producer\n```\n\n## Producer Server (Port 3000)\n\nThe producer server exposes a POST route (`/rabbitmq`) that generates fake data using [@faker-js/faker](https://www.npmjs.com/package/@faker-js/faker) and publishes two messages—one for a user and one for a country—to the exchange with the routing keys `\"users\"` and `\"countries\"` respectively.\n\n**Example code (producer):**\n\n```js\n\"use strict\";\n\nconst { faker } = require(\"@faker-js/faker\");\n\n/** @param {import(\"fastify\").FastifyInstance} fastify */\nmodule.exports = async function (fastify) {\n  fastify.post(`/`, {}, async function (_, reply) {\n    const fakeUser = {\n      id: faker.string.uuid(),\n      username: faker.internet.username(),\n      email: faker.internet.email(),\n      createdAt: new Date().toISOString(),\n    };\n\n    const fakeCountry = {\n      id: faker.string.uuid(),\n      country: faker.location.country(),\n      city: faker.location.city(),\n    };\n\n    await Promise.all([\n      fastify.rabbitmqPublisher.send(\n        { exchange: process.env.RABBITMQ_EXCHANGE, routingKey: \"users\" },\n        fakeUser\n      ),\n      fastify.rabbitmqPublisher.send(\n        { exchange: process.env.RABBITMQ_EXCHANGE, routingKey: \"countries\" },\n        fakeCountry\n      ),\n    ]);\n\n    reply.send({ user: fakeUser, country: fakeCountry });\n  });\n};\n```\n\n## Consumer‑1 Server (Port 3001)\n\nConsumer‑1 registers a consumer on the queue `q.consumer-1` with a binding for the `\"users\"` routing key, so it only receives user messages.\n\n**Example code (consumer‑1):**\n\n```js\nconst fp = require(\"fastify-plugin\");\nconst rabbitMQPlugin = require(\"fastify-rabbitmq\");\n\nmodule.exports = fp(\n  async function (fastify) {\n    fastify.register(rabbitMQPlugin, {\n      connection: process.env.RABBITMQ_CONNECTION,\n    });\n\n    fastify.ready().then(async () =\u003e {\n      fastify.rabbitmq.createConsumer(\n        {\n          queue: \"q.consumer-1\",\n          queueOptions: { durable: true },\n          queueBindings: [\n            { exchange: process.env.RABBITMQ_EXCHANGE, routingKey: \"users\" },\n          ],\n        },\n        async (msg) =\u003e {\n          console.log(\"Consumer-1 received message:\", msg);\n        }\n      );\n    });\n  },\n  { name: \"rabbitmq\" }\n);\n```\n\n## Consumer‑2 Server (Port 3002)\n\nConsumer‑2 registers a consumer on the queue `q.consumer-2` with bindings for both `\"users\"` and `\"countries\"` routing keys, so it receives messages for both.\n\n**Example code (consumer‑2):**\n\n```js\nconst fp = require(\"fastify-plugin\");\nconst rabbitMQPlugin = require(\"fastify-rabbitmq\");\n\nmodule.exports = fp(\n  async function (fastify) {\n    fastify.register(rabbitMQPlugin, {\n      connection: process.env.RABBITMQ_CONNECTION,\n    });\n\n    fastify.ready().then(async () =\u003e {\n      fastify.rabbitmq.createConsumer(\n        {\n          queue: \"q.consumer-2\",\n          queueOptions: { durable: true },\n          queueBindings: [\n            { exchange: process.env.RABBITMQ_EXCHANGE, routingKey: \"users\" },\n            {\n              exchange: process.env.RABBITMQ_EXCHANGE,\n              routingKey: \"countries\",\n            },\n          ],\n        },\n        async (msg) =\u003e {\n          console.log(\"Consumer-2 received message:\", msg);\n        }\n      );\n    });\n  },\n  { name: \"rabbitmq\" }\n);\n```\n\n## Running the Project\n\n1. **Configure Environment Variables:**\n\n   In each server directory (`producer`, `consumer-1`, and `consumer-2`), create a `.env` file with:\n\n   ```env\n   RABBITMQ_CONNECTION=amqp://guest:guest@localhost\n   RABBITMQ_EXCHANGE=ex.producer\n   ```\n\n2. **Install Dependencies:**\n\n   Run your package manager (e.g. `npm install` or `pnpm install`) in each directory.\n\n3. **Start the Servers:**\n\n   - Start the producer server on port 3000.\n   - Start consumer‑1 on port 3001.\n   - Start consumer‑2 on port 3002.\n\n4. **Test the Setup:**\n\n   Send a POST request to `http://localhost:3000/rabbitmq`.  \n   The producer will publish fake user and country messages, consumer‑1 will log only user messages, and consumer‑2 will log both.\n\n## Summary\n\nThis project shows how to build a simple distributed system with Fastify and RabbitMQ:\n\n- The **producer** generates fake data and publishes messages.\n- **Consumer‑1** listens for user messages.\n- **Consumer‑2** listens for both user and country messages.\n\nFeel free to extend this example or modify it to suit your needs!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xomaradel%2Ffastify-rabbitmq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xomaradel%2Ffastify-rabbitmq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xomaradel%2Ffastify-rabbitmq/lists"}