First implementation of WebSub

This commit is contained in:
2021-11-10 15:14:41 +01:00
parent f2fb945064
commit 18fdf819f1
11 changed files with 328 additions and 5 deletions

29
mocks/README.md Normal file
View 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

View 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()

View 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()

View 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()