First implementation of WebSub
This commit is contained in:
29
mocks/README.md
Normal file
29
mocks/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
In this directory are some files to mock an auction house to test WebSub local.
|
||||
|
||||
To run a local WebSubHub instance
|
||||
|
||||
1. Start a mongodb in docker:
|
||||
|
||||
- docker run -d -p 27017:27017 -p 28017:28017 -e AUTH=no tutum/mongodb
|
||||
|
||||
2. Install a local hub
|
||||
|
||||
- yarn global add websub-hub
|
||||
|
||||
3. Run the hub localy
|
||||
|
||||
- websub-hub -l info -m mongodb://localhost:27017/hub
|
||||
|
||||
Create an example subscription
|
||||
|
||||
- node auction-house/subscriber.js
|
||||
|
||||
Create an example auctionhouse
|
||||
|
||||
- node auction-house/auctions.js
|
||||
|
||||
Publish to the hub
|
||||
|
||||
- node auction-house/publisher.js
|
||||
|
||||
Mostly inspired by: https://github.com/hemerajs/websub-hub
|
39
mocks/auction-house/auctions.js
Normal file
39
mocks/auction-house/auctions.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// Require the framework and instantiate it
|
||||
const fastify = require('fastify')({ logger: true })
|
||||
|
||||
// Declare a route
|
||||
fastify.get('/auctions', async (request, reply) => {
|
||||
console.log('content provided')
|
||||
|
||||
return [
|
||||
{
|
||||
id: '2',
|
||||
content_text: 'This is a second item.',
|
||||
url: 'https://example.org/second-item'
|
||||
},
|
||||
{
|
||||
id: '1',
|
||||
content_html: '<p>Hello, world!</p>',
|
||||
url: 'https://example.org/initial-post'
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
fastify.get('/websub', async (request, reply) => {
|
||||
console.log('content provided')
|
||||
|
||||
return {
|
||||
topic: 'http://localhost:3100/auctions'
|
||||
}
|
||||
})
|
||||
|
||||
// Run the server!
|
||||
const start = async () => {
|
||||
try {
|
||||
await fastify.listen(3100)
|
||||
} catch (err) {
|
||||
fastify.log.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
start()
|
17
mocks/auction-house/publisher.js
Normal file
17
mocks/auction-house/publisher.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const axios = require('axios').default
|
||||
|
||||
// Run the server!
|
||||
const start = async () => {
|
||||
await axios
|
||||
.post('http://localhost:3000/publish', {
|
||||
'hub.mode': 'publish',
|
||||
'hub.url': 'http://localhost:3100/auctions'
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response.data)
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
start()
|
42
mocks/auction-house/subscriber.js
Normal file
42
mocks/auction-house/subscriber.js
Normal file
@@ -0,0 +1,42 @@
|
||||
// Require the framework and instantiate it
|
||||
const fastify = require('fastify')({ logger: true })
|
||||
const axios = require('axios').default
|
||||
|
||||
// Declare a route
|
||||
fastify.get('/auction-created', async (request, reply) => {
|
||||
console.log('subscription verified', request.query)
|
||||
console.log(request.query)
|
||||
return request.query
|
||||
})
|
||||
|
||||
fastify.post('/auction-created', async (request, reply) => {
|
||||
console.log('received blog content', request.body)
|
||||
reply.send()
|
||||
})
|
||||
|
||||
// Run the server!
|
||||
const start = async () => {
|
||||
// subscribe to the feed
|
||||
|
||||
try {
|
||||
await fastify.listen(3200)
|
||||
} catch (err) {
|
||||
fastify.log.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
await axios
|
||||
.post('http://localhost:3000', {
|
||||
'hub.callback': 'http://localhost:3200/auction-created',
|
||||
'hub.mode': 'subscribe',
|
||||
'hub.topic': 'http://localhost:3100/auctions',
|
||||
'hub.ws': false
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response.data)
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
start()
|
Reference in New Issue
Block a user