Skip to content

Commit

Permalink
docs: new serverless queue example
Browse files Browse the repository at this point in the history
  • Loading branch information
dreyacosta committed Sep 14, 2024
1 parent 1ca01e0 commit 2ead5aa
Show file tree
Hide file tree
Showing 22 changed files with 4,202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/serverless-queue/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NIXBUS_TOKEN=kcvar1ogctxwkjf71p1p2141
NIXBUS_PASSPHRASE=M38rmVJNh6pc2B4rE6fvwsgoUY
Empty file.
8 changes: 8 additions & 0 deletions examples/serverless-queue/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions examples/serverless-queue/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/serverless-queue/.idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/serverless-queue/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/serverless-queue/.idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/serverless-queue/.idea/serverless-queue.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/serverless-queue/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/serverless-queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Serverless queue example with NixBus

1. Install dependencies:
```bash
npm install
```

2. Set the environment variables:
```bash
cp .env.example .env
```
Edit the `.env` file and set the `NIXBUS_TOKEN` and `NIXBUS_PASSPHRASE` variables.

2. Run the example:
```bash
npm start
```
34 changes: 34 additions & 0 deletions examples/serverless-queue/bin/consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getNixBusHttp } from '@nixbus/event-bus'

Check failure on line 1 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module '@nixbus/event-bus' or its corresponding type declarations.

import { SendPageViewedNotification } from 'src/actions/SendPageViewedNotification'

Check failure on line 3 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/actions/SendPageViewedNotification' or its corresponding type declarations.
import { UpdatePageViewedAnalytics } from 'src/actions/UpdatePageViewedAnalytics'

Check failure on line 4 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/actions/UpdatePageViewedAnalytics' or its corresponding type declarations.
import { PageViewed } from 'src/events/PageViewed'

Check failure on line 5 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/events/PageViewed' or its corresponding type declarations.
import { getEnvVar } from 'src/shared/env'

Check failure on line 6 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/shared/env' or its corresponding type declarations.

async function main() {
const bus = getNixBusHttp({
token: getEnvVar('NIXBUS_TOKEN'),
passphrase: getEnvVar('NIXBUS_PASSPHRASE'),
})

await bus.unsubscribeAll()
await Promise.all([
bus.subscribe(PageViewed.getEventType(), {
id: SendPageViewedNotification.name,
action: SendPageViewedNotification,
config: { maxRetries: 3, timeout: 30, concurrency: 10 },
}),
bus.subscribe(PageViewed.getEventType(), {
id: UpdatePageViewedAnalytics.name,
action: UpdatePageViewedAnalytics,
config: { maxRetries: 3, timeout: 30, concurrency: 10 },
}),
])

bus
.run()
.then(() => console.log('[Consumer] Running NixBus...'))
.catch((error) => console.error('[Consumer] Error:', error))

Check failure on line 31 in examples/serverless-queue/bin/consumer.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Parameter 'error' implicitly has an 'any' type.
}

main().catch(console.error)
29 changes: 29 additions & 0 deletions examples/serverless-queue/bin/publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getNixBusHttp } from '@nixbus/event-bus'

Check failure on line 1 in examples/serverless-queue/bin/publisher.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module '@nixbus/event-bus' or its corresponding type declarations.

import { PageViewed } from 'src/events/PageViewed'

Check failure on line 3 in examples/serverless-queue/bin/publisher.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/events/PageViewed' or its corresponding type declarations.
import { getEnvVar } from 'src/shared/env'

Check failure on line 4 in examples/serverless-queue/bin/publisher.ts

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Cannot find module 'src/shared/env' or its corresponding type declarations.

async function main() {
const bus = getNixBusHttp({
token: getEnvVar('NIXBUS_TOKEN'),
passphrase: getEnvVar('NIXBUS_PASSPHRASE'),
})

while (true) {
const title = `Title ${getTitleId()}`
console.log(`[Publisher] Publishing ${PageViewed.getEventType()}: ${title}`)
await bus.publish(
PageViewed.create({
title,
}),
)
await new Promise((resolve) => setTimeout(resolve, 250))
}
}

let titleId = 1
function getTitleId(): number {
return titleId++
}

main().catch(console.error)
62 changes: 62 additions & 0 deletions examples/serverless-queue/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import js from '@eslint/js'
import noRelativeImportPaths from 'eslint-plugin-no-relative-import-paths'
import simpleImportSort from 'eslint-plugin-simple-import-sort'
import unusedImports from 'eslint-plugin-unused-imports'
import globals from 'globals'
import ts from 'typescript-eslint'

export default [
js.configs.recommended,
...ts.configs.recommended,
{
plugins: {
'no-relative-import-paths': noRelativeImportPaths,
'simple-import-sort': simpleImportSort,
'unused-imports': unusedImports,
},

languageOptions: {
ecmaVersion: 2022,
globals: {
...globals.browser,
...globals.node,
},
parserOptions: {
project: true,
},
sourceType: 'module',
},

rules: {
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': 'warn',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '__',
caughtErrorsIgnorePattern: '__',
},
],
'@typescript-eslint/no-var-requires': 'off',
'no-relative-import-paths/no-relative-import-paths': 'error',
'no-unused-vars': 'off',
'simple-import-sort/imports': [
'error',
{
groups: [
['^\\u0000'],
['^node:.*\\u0000$', '^node:'],
['^(?!src)@?\\w.*\\u0000$', '^(?!src)@?\\w'],
['^src.*\\u0000$', '^src'],
['(?<=\\u0000)$', '^'],
['^\\..*\\u0000$', '^\\.'],
],
},
],
'unused-imports/no-unused-imports': 'error',
},
},
]
Loading

0 comments on commit 2ead5aa

Please sign in to comment.