Created
February 28, 2022 15:45
-
-
Save miton18/b139c94ff8ec884d8727e7ed93d9e646 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
import WebSocket from 'websocket' | |
const tenant = 'user_xxx' | |
const namespace = 'pulsar_xxx' | |
const topic = 'sometopic' | |
const token = process.env.PULSAR_TOKEN | |
const producerURL = `wss://c2-pulsar-clevercloud-customers.services.clever-cloud.com:2000/ws/v2/producer/persistent/${tenant}/${namespace}/${topic}?token=${token}&producerName=toto` | |
const msg = { | |
"payload": "SGVsbG8gV29ybGQ=", | |
"properties": {"key1": "value1", "key2": "value2"}, | |
"context": "1" | |
} | |
const c = new WebSocket.client() | |
c.on('connectFailed', err => console.log(`Connect Error: ${err.toString()}`)) | |
c.on('connect', connection => { | |
console.log('WebSocket Client Connected') | |
connection.on('error', error => { | |
console.log("Connection Error: " + error.toString()); | |
}) | |
connection.on('close', () => { | |
console.log('echo-protocol Connection Closed') | |
}) | |
connection.on('message', message => { | |
if (message.type === 'utf8') { | |
console.log("Pulsar: '" + message.utf8Data + "'") | |
} | |
}) | |
if (connection.connected) { | |
connection.sendUTF(JSON.stringify(msg)) | |
} else { | |
console.error('not connected') | |
} | |
}) | |
console.log(producerURL) | |
c.connect(producerURL, 'echo-protocol') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment