meds
meds

Reputation: 22916

Possible to connect to an azure event hub through a browser and websockets?

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?

Upvotes: 1

Views: 2808

Answers (1)

Peter Pan
Peter Pan

Reputation: 24128

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.

Upvotes: 2

Related Questions