# gcp-pub-sub

> Typescript support for gcp's pub-sub

Latest version **0.0.2** (published 2022-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install gcp-pub-sub
pnpm add gcp-pub-sub
yarn add gcp-pub-sub
bun add gcp-pub-sub
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2022-03-31 |
| First published | 2022-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | chirgjin |
| Maintainers | chirgjin |
| Keywords | gcp, pub-sub, gcp-pub-sub |

## Links

- npm: https://www.npmjs.com/package/gcp-pub-sub
- Repository: https://github.com/chirgjin/gcp-pub-sub
- Homepage: https://github.com/chirgjin/gcp-pub-sub#readme
- Issues: https://github.com/chirgjin/gcp-pub-sub/issues
- npm.io page: https://npm.io/package/gcp-pub-sub

## Dependencies (1)

- [@poppinss/hooks](https://npm.io/package/@poppinss/hooks.md) ^5.0.2

## Recent versions

- 0.0.2 (latest) — 2022-03-31
- 0.0.1 — 2022-03-31

## README

# gcp-pub-sub
Easy to use google's pub sub event system for typescript

# Introduction
This package aims to implement a event based system using Google Cloud Platform's pub/sub system.


### Initialization
PubSub needs to be configured before it can be used.
For the authentication part, refer to [google-auth-library](https://www.npmjs.com/package/google-auth-library).

*Make sure this file is loaded before you declare any events*
```ts
import { config } from 'gcp-pub-sub'

config.init({
    subscription: 'my-subscription-name', // this can also be a function to generate subscription name on runtime
    projectId: 'my-gcp-project-id', // this is optional. If not provied then it will be inferred from your credentials
    defaultTopic: 'my-topic', // This topic will be used for all events by default. However, you can also provide a different topic when creating events
})
```

### Creating Events
Events are created using `event` function.
Example:

```ts
import { event } from 'gcp-pub-sub'

export const myEvent = event<{
    someEventData: string
    anotherEventData: string
}>().create('myEvent')
```

### Consuming Events

events.ts
```ts
import { event } from 'gcp-pub-sub'

export const myEvent = event<{
    someEventData: string
    anotherEventData: string
}>().create('myEvent')

myEvent.subscribe((eventData) => {
    console.log(
        `SomeEventData = ${eventData.data.someEventData}, EventName = ${eventData.attributes.event}`
    )
})
```

consumer.ts
```ts
import './events.ts' // to make sure events are loaded in memory
import { runConsumer } from 'gcp-pub-sub'

runConsumer()
```

then execute
```bash
ts-node consumer.ts
```

---
_Source: https://npm.io/package/gcp-pub-sub · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
