I am writing an excel add-in to receive messages through SignalR with SignalR broadcast messages to a group. I have my backend functions and frontend working properly without using the groups. But after I added group to it, it is not working. For my backend, I have a AddToGroup function:
[Function("AddToGroup")]
[SignalROutput(HubName = "chatHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public static async Task<SignalRGroupAction> AddToGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req, FunctionContext context)
{
var logger = context.GetLogger("AddToGroup");
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var data = JsonConvert.DeserializeObject<AddToGroupData>(requestBody);
logger.LogInformation($"Request Data: UserId = {data.UserId}, GroupName = {data.GroupName}");
return new SignalRGroupAction(SignalRGroupActionType.Add)
{
UserId = data.UserId,
GroupName = data.GroupName
};
}
catch (Exception ex)
{
logger.LogError($"Exception occurred in AddToGroup: {ex.Message}");
throw;
}
}
And for my frontend, I am communicating with this part:
connection.start()
.then(() => {
console.log("Connected to SignalR hub.");
// After connection, join the group
fetch("http://localhost:7146/api/AddToGroup", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
UserId: connection.connectionId,
GroupName: "Group1",
}),
})
.then(response => response.json())
.then(data => console.log("Joined group", data))
.catch(error => console.error("Error joining group", error));
})
.catch(err => console.error('Error during connection: ', err));
But when I test for my codes, it is only showing "Joined group Group1". But when I send messages, there is no display of message on my taskpane in excel. What could be the problem?
I have tried the connection.invoke method, but it is totally not working.
Joining a SignalR group typically doesn't return data like a standard HTTP request. Instead, it might just indicate success or failure. Your server-side function
AddToGroupreturns aSignalRGroupAction, but the client side treats it as JSON.ChatHub.cs:
Server-Side Code (Azure Function):
Client-Side Code (Excel Add-In):
AddToGroupmethod is invoked on the server to add the Excel add-in to the specified group ("Group1"). Then, when a message is sent to the group, the clients in that group will receive the message.Checking the console logs in the browser developer tools to ensure the connection is successful and that you are joining the group.
Send a message from the backend to the group. The received message should be logged in the browser console.