Created
September 10, 2021 07:20
-
-
Save Sraleik/465d275c4aa13ce4418db82de4261389 to your computer and use it in GitHub Desktop.
clever-pulsar-websocket
This file contains 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
import WebSocket from 'ws' | |
// If set enableTLS to true, your have to set tlsEnabled to true in conf/websocket.conf. | |
const enableTLS = true; | |
const token = 'fake-token' | |
const topic = `${enableTLS ? 'wss' : 'ws'}://c2-pulsar-clevercloud-customers.services.clever-cloud.com:2000/ws/v2/producer/persistent/user_5db919b8-f988-4c72-a269-f8d0f62cef22/pulsar_39f5266a-b0ab-4441-a787-18f897afa796/test`; | |
const ws = new WebSocket(topic, { | |
headers: { | |
Authorization: `Bearer ${token}` | |
} | |
}); | |
var message = { | |
"payload" : Buffer.from("Hello World").toString('base64'), | |
"properties": { | |
"key1" : "value1", | |
"key2" : "value2" | |
}, | |
"context" : "1" | |
}; | |
ws.on('open', function() { | |
// Send one message | |
ws.send(JSON.stringify(message)); | |
}); | |
ws.on('message', function(message) { | |
console.log('received ack: %s', message); | |
}); | |
ws.on('error', function(error) { | |
console.log("Error: ", error) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment