0

I'm trying to implement a web based chat program, my plan is to update an azure cosmos database with chat messages, then push an event to an azure event hub.

The missing element is for connected web browsers to receive these events.

I tried using the azure event hubs npm package (https://www.npmjs.com/package/@azure/event-hubs) but that looks like it's server side.

Is there a way to accomplish this without having to spin up some sort of server or service?

meds
  • 21,699
  • 37
  • 163
  • 314

1 Answers1

2

It sounds like you want to diectly connect EventHub via a browser with websocket, as I known, the only way to realize it is to use AMQP over WebSocket to connect EventHub.

There is a MSDN blog introduce this topic Connect to Azure Event Hub in browser ( using AMQP over WebSockets ), and the other blog of the same author introduce How to use AMQP protocol in Browser (JavaScript). And you can get rhea.js from its GitHub repo https://github.com/amqp/rhea/tree/master/dist.

Meanwhile, according to the information from the source codes of @azure/event-hubs, it seems to support the feature of AMQP over WebSocket in browser, as the figure below comes from the source code https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/eventhub/event-hubs/src/impl/eventHubClient.ts#L259.

enter image description here

And there is a sample code of websockets.ts for EventHubs, and it requires @azure/event-hubs version next in its package.json. I think you just need to use WebSocket in browser instead of WebSocket from import WebSocket from "ws"; in the sample, then you could make it works in browser.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • 1
    A small update to this answer - You no longer need the `next` version of the `@azure/event-hubs` package. These features are available on the latest version since Jan 2020 - The sample link provided above is broken as the code got moved around. You can refer to https://github.com/Azure/azure-sdk-for-js/blob/%40azure/event-hubs_5.2.1/sdk/eventhub/event-hubs/samples/javascript/websockets.js instead – Ramya Rao Jul 21 '20 at 05:54
  • example link is broken – Evandro Pomatti Mar 12 '21 at 11:16