Azure / azure-webpubsub

Azure Web PubSub Service helps you to manage WebSocket connections and do publish and subscribe in an easy way
https://azure.github.io/azure-webpubsub/
MIT License
135 stars 85 forks source link

Abuse protection failed when using Azure Function #163

Closed amolenk closed 3 years ago

amolenk commented 3 years ago

Describe the bug

I'm trying to use Web PubSub in combination with a C# Function App in Azure. When I sent a custom event to the Function App, the abuse protection fails with status code 401.

If I run the Function locally, use ngrok, and change the event handler to the ngrok URL, everything works fine.

To Reproduce

I've created a Function App called serverlessponies1805. In the trace logging, I see the following message:

Registered Web PubSub negotiate Endpoint = https://serverlessponies1805.azurewebsites.net/runtime/webhooks/webpubsub

I used that URL to set up my event handler.

I then connect succesfully to the Web PubSub service from a client application and send a custom event.

The live trace now shows the following error:

Event Name = AbuseProtectionResponseInvalidStatusCode
Message = Abuse protection failed: 401

What am I missing here?

JialinXin commented 3 years ago

Have you included the function keys when setup the event hander? This is required when function app is hosted in Azure. See snapshot below to get the registered key from Function App. Including the key, the event handler url should be like https://{function-app}.azurewebsites.net/runtime/webhooks/webpubsub?code={webpubsub_extension_key}

image

There's a known issue when authenticated enabled will always return 401. If the function key is not the reason for you case, can you share more information in your scenario? Share a sample is better.

amolenk commented 3 years ago

Thanks! Works perfect now that I use the function key. Has this been documented somewhere already? I couldn't find any docs/samples on hosting a Web PubSub function app in Azure.

JialinXin commented 3 years ago

Sorry that after a few rounds of document refactor, I missed to add it anywhere. Basically it should be mentioned in both Azure Function side similar to this and our documents side. I'll put it in near future. Thanks for the feedbacks!

anzax commented 3 years ago

Hey @JialinXin, it would be really nice to update documentation. I've just spent 2h debugging until I found this.

JialinXin commented 3 years ago

@anzax , Thanks for adding comments. I'm working on this. BTW, to help improve the noticing entry, would you share some insights about which doc are you use to work with? I'll add some link to it to help navigation.

anzax commented 3 years ago

@JialinXin, I was using this sources of documentation:

I got it working locally with ngrok but it didn't work when I deployed function app as I didn't know there is a need to specify security code in the webhook url. Moreover, Azure Web PubSub service was returning 500 on an attempt to connect to websockets so it was hard to figure out what was wrong.

JialinXin commented 3 years ago

For short, I'm updating the sample related to event-handler settings for some further introduction. Besides, we need to add a few more in Azure Docs. Keep the issue open until we finished.

https://github.com/Azure/azure-webpubsub/tree/main/samples/functions/js/simplechat

JialinXin commented 3 years ago

Added in docs already.

https://docs.microsoft.com/en-us/azure/azure-web-pubsub/quickstart-serverless?tabs=javascript#configure-the-web-pubsub-service-event-handler