A minimal example of running Apache Pulsar with websockets server and client side (in a browser)
Clone this repo and run
docker-compose up -d
npm install
node producer.js
Open your favourite browser's console, and paste in the following Javascript:
// http://pulsar.apache.org/docs/en/client-libraries-websocket.html#nodejs-consumer
var websocket = new WebSocket("ws://localhost:38080/ws/v2/consumer/persistent/public/default/my-topic/my-sub")
websocket.onopen = (event) => {
console.log("Connected!")
}
websocket.onmessage = (event) => {
j = JSON.parse(event.data)
console.log("Received message", j)
console.log("Payload: ", atob(j.payload)) // Base64 decode
}
// websocket.close()
This code can only be run once at a time. Run
websocket.close()
if you want to start a new consumer.