Skip to content

Commit

Permalink
Merge pull request #584 from ccoeurderoy/feat/add-examples
Browse files Browse the repository at this point in the history
[FEAT] New unsubscribe method + Example app
  • Loading branch information
meriamBenSassi authored Feb 16, 2024
2 parents ba885c7 + 3ebb23c commit d04bce8
Show file tree
Hide file tree
Showing 11 changed files with 2,575 additions and 528 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ If the chosen transport is Google Cloud PubSub, then `options` would be:
- `topicOptions`: Options applied to the created topic (have a look at [Topic options](https://googleapis.dev/nodejs/pubsub/latest/Topic.html#get))
- `publishOptions`: Publish options set to the topic after its creation. Refer to [Publish Options](https://googleapis.dev/nodejs/pubsub/latest/global.html#PublishOptions)
- `messageOptions`: Additional message options added to the message. Refer to [Message Options](https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.IPubsubMessage.html)

### `pubsub.unsubscribe(event)`

Stop the server connection for a given subscription.

- `event`: Name of of the event to stop listening for.
34 changes: 34 additions & 0 deletions examples/google-pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# PubSub Example: Google Cloud Pub/Sub

Sample Express application in order to test the @algoan/pubsub library with Google Pub/Sub.

## Getting Started

Install dependencies:

```shell
npm i
```

Run a google Pub/Sub emulator using gcloud CLI. You must ensure that `beta` and `pubsub-emulator` are installed:

```shell
gcloud components install pubsub-emulator
gcloud components install beta
gcloud components update
````

You can also run the emulator using Docker:

```shell
docker pull google/cloud-sdk:emulators
docker run --rm -p 8085:8085 google/cloud-sdk:emulators /bin/bash -c "gcloud beta emulators pubsub start --project=test --host-port='0.0.0.0:8085'"
```

Then, run:

```shell
npm start
```

Navigate on http://localhost:3000! To emit an event, simply call GET http://localhost:3000/emit. To close the subscription server collection, call the GET http://localhost:3000/close API.
53 changes: 53 additions & 0 deletions examples/google-pubsub/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
process.env.PUBSUB_EMULATOR_HOST = 'localhost:8085';
process.env.PUBSUB_PROJECT_ID = 'test';

const express = require('express');
const timers = require('timers/promises');
const app = express();
const port = 3000;
const Pubsub = require('@algoan/pubsub');
const topicName = 'my_topic';

const pubsubClient = Pubsub.PubSubFactory.create({
options: {
projectId: 'test',
},
});
const secondPubsubClient = Pubsub.PubSubFactory.create({
options: {
projectId: 'test',
},
});

let pubsubCall = 0;

app.get('/', (req, res) => {
res.send(`PubSub calls: ${pubsubCall}`);
});

app.get('/emit', async (req, res) => {
secondPubsubClient.emit(topicName, {});
await timers.setTimeout(1000);
res.redirect('/');
});

app.get('/close', async (req, res) => {
await pubsubClient.unsubscribe(topicName);
await timers.setTimeout(1000);
res.redirect('/');
});

app.listen(port, async () => {
await pubsubClient.listen(topicName, {
options: {
subscriptionOptions: {
name: topicName,
},
},
onMessage: () => {
console.log('Received message!');
pubsubCall++;
},
});
console.log(`Example app listening on port ${port}`);
});
Loading

0 comments on commit d04bce8

Please sign in to comment.