-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #584 from ccoeurderoy/feat/add-examples
[FEAT] New unsubscribe method + Example app
- Loading branch information
Showing
11 changed files
with
2,575 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
Oops, something went wrong.