-
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.
chore(examples): add an example app to test google pubsub
- Loading branch information
1 parent
ba885c7
commit e95d83f
Showing
4 changed files
with
1,909 additions
and
1 deletion.
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
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}`) | ||
}); |
Oops, something went wrong.