Azure-Samples / signalr-service-quickstart-serverless-chat

Serverless chat quickstart samples for Azure SignalR
MIT License
75 stars 62 forks source link

No documentation on Users/Groups #18

Open tstpierre opened 4 years ago

tstpierre commented 4 years ago

How in an Azure Function in node do we access user/connect info and add to a group to control how many messages we actually broadcast out?

Is there a server side hub npm package (much like @microsoft/signalr is for clients) that give us a better sense of whats available?

tstpierre commented 4 years ago

I found this bindings documentation for the function itself, but is there a server sdk to assist with creating the groups, etc.

Or are we left with doing REST calls to the signalR service directly?

https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-functions/functions-bindings-signalr-service-output.md#javascript-2

malteA commented 4 years ago

Hi,

i had the same issue and resolved it like this:

const connection: HubConnection = new HubConnectionBuilder()
    .withUrl(process.env.REACT_APP_SIGNALR_CONNECTION + "?userId=[USERID]")
    .configureLogging(LogLevel.Information)
    .build();
[FunctionName("negotiate")]
public static SignalRConnectionInfo GetSignalRInfo(
  [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
  [SignalRConnectionInfo(HubName = "chat", UserId = "{query.userId}" )] SignalRConnectionInfo connectionInfo)
{
  return connectionInfo;
}

I didnt find a way to attach it to the header. This the source code from the @microsoft/signalr npm package it seems like there is no way to attach a custom header.

private async getNegotiationResponse(url: string): Promise<INegotiateResponse> {
        let headers;
        if (this.accessTokenFactory) {
            const token = await this.accessTokenFactory();
            if (token) {
                headers = {
                    ["Authorization"]: `Bearer ${token}`,
                };
            }
        }

        const negotiateUrl = this.resolveNegotiateUrl(url);
        this.logger.log(LogLevel.Debug, `Sending negotiation request: ${negotiateUrl}.`);
        try {
            const response = await this.httpClient.post(negotiateUrl, {
                content: "",
                headers,
            });

I hope this helps you a bit.

halter73 commented 4 years ago

In @microsoft/signalr 5.0, a headers option will be added. https://docs.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-5.0&tabs=javascript#configure-client-options

DarrenWainwright commented 3 years ago

In addition to the above (thank you for the great question)

When using SignalR Service in serverless and a function app running in Javascript, how do you add users to a group?

Do i need to create a specific function? What should this look like? Specific documentation if possible please :) I've been grappling for days...

Thanks

halter73 commented 3 years ago

@DarrenWainwright We should definitely update this sample to use groups. In the meantime, have you seen https://github.com/Azure/azure-signalr/blob/1dcfaab8ee90f9a18f96f9bdb7f0a25ab72a8d70/docs/management-sdk-guide.md?

Edit: I now see you're looking for something for JS. Maybe take a look at https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-output?tabs=javascript#group-management