Skip to content

Commit

Permalink
chore(examples): add an example app to test google pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoeurderoy committed Feb 14, 2024
1 parent ba885c7 commit e95d83f
Show file tree
Hide file tree
Showing 4 changed files with 1,909 additions and 1 deletion.
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.client.close();
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 e95d83f

Please sign in to comment.