Skip to content

Instantly share code, notes, and snippets.

@miton18
Created February 28, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miton18/b139c94ff8ec884d8727e7ed93d9e646 to your computer and use it in GitHub Desktop.
Save miton18/b139c94ff8ec884d8727e7ed93d9e646 to your computer and use it in GitHub Desktop.
#!/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