fp/packages/realtime/index.js

45 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-05-20 23:47:14 +00:00
const http = require('node:http');
const faye = require('faye');
const url = require('url');
const pkg = require('./package.json')
require('dotenv').config()
var server = http.createServer(),
bayeux = new faye.NodeAdapter({ mount: '/faye' });
bayeux.on('subscribe', function (clientId, channel) {
console.log(`bayeux client ${clientId} subscribed to ${channel}`)
})
const client = bayeux.getClient()
if (process.env.NODE_ENV === 'development') {
function publish(msg) {
client.publish('/signals', {
text: msg
})
}
setInterval(() => {
publish('Hello mocha!')
}, 1000);
}
server.on('request', (req, res) => {
const parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname === '/metrics') {
// Handle the /metrics route
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`version=${pkg.version}`);
}
});
bayeux.attach(server);
const port = process.env.PORT || 5000;
console.log(`listening on ${port}`);
server.listen(port);