4 * Acts as a bridge for forwarding UDP packets on port 6000 to PubSub.
6 * Listens on UDP [::1]:6000 by default, can be overriden with the first and
7 * second command line argument, respectively. No legacy IP support.
9 * Forwards the port number as an attribute (with the name "port") to the
10 * pubsub messages, for ingestion of data from multiple bodet.js instances to
13 * See README for required credential setup.
17 const {PubSub} = require('@google-cloud/pubsub');
18 const dgram = require('dgram');
20 const topic = 'projects/plastkast/topics/ultimatescore';
21 const host = process.argv[2] || '::1';
22 const port = process.argv[3] || 6000;
24 const pubsub = new PubSub();
26 const sock = dgram.createSocket('udp6');
27 sock.on('listening', () => {
28 const addr = sock.address().address;
29 console.log(`UDP listening on [${addr}]:${port}`);
32 sock.on('message', processPacket);
34 async function processPacket(msg, rinfo) {
35 console.log(`UDP datagram from ${rinfo.address}:${port}: ${msg}`);
36 const attr = { port: port.toString() };
37 const id = await pubsub.topic(topic).publish(msg, attr);
38 console.log(`PubSub message published with ID ${id}`);
41 sock.bind(port, host);