@Liangying Wei
I think there has been a misunderstanding; I have a fully configured and functional locally hosted SignalR application that uses the SignalR emulator. All my server functions are being invoked via the client, including my negotiate function. None of this would be possible if my core setup, including my AzureSignalRConnectionString or SignalR service was not configured correctly. Maybe there is something explicit that requires a real SignalR service (via Azure)? If its not the case I am all good setup wise.
As for the issue at hand, I have modified the Negotiate function as per your specifications by adding the UserId = "user1" parameter.
As a reminder of where we left off, here is my full code (server and client)
Server side code (includes modified Negotiate function):
public class Functions
{
[Function("Negotiate")]
public static string Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
[SignalRConnectionInfoInput(HubName = "serverless", UserId = "user1")] string connectionInfo)
{
return connectionInfo;
}
// This gets invoked though I don't believe that the SignalRGroupAction(SignalRGroupActionType.Add) below actually adds the user to the group
[Function("AddToGroup")]
[SignalROutput(HubName = "serverless", ConnectionStringSetting = "AzureSignalRConnectionString")]
// string userId is NULL here
public static async Task<SignalRGroupAction> AddToGroup([SignalRTrigger("serverless", "messages", "AddToGroup", "groupName", "userId")] SignalRInvocationContext invocationContext, string groupName, string userId)
{
// This does not seem to add the user to the group. Note I do not have any on the client side for this. Not sure if I need one?
return new SignalRGroupAction(SignalRGroupActionType.Add)
{
GroupName = groupName,
UserId = userId
};
}
[Function("SendToGroup")]
[SignalROutput(HubName = "serverless", ConnectionStringSetting = "AzureSignalRConnectionString")]
public static async Task<SignalRMessageAction> SendToGroup([SignalRTrigger("serverless", "messages", "SendToGroup", "groupName", "message")] SignalRInvocationContext invocationContext, string groupName, string message)
{
// The the client does NOT receive this returned SignalRMessageAction (see cient code below):
// _connection.On<string>("joinedGroupMessage", (groupMessage) => { chatMessagesX.Text = groupMessage; });
return new SignalRMessageAction("joinedGroupMessage")
{
// If I comment out the GroupName argument below the client WILL received the SignalRMessageAction result
GroupName = groupName,
Arguments = new object[] { $"Sent {message} to group {groupName}." }
};
}
}
Client side code - when I run it I first invoke AddToGroup then SendToGroup (in that specific order):
public partial class MainPage : ContentPage
{
private readonly HubConnection _connection;
private string _url = "http://localhost:7071/api";
public class GroupData
{
public string GroupName { get; set; }
public string UserId { get; set; }
}
static GroupData _groupData = new GroupData();
public MainPage()
{
InitializeComponent();
_connection = new HubConnectionBuilder().WithUrl(_url).Build();
_groupData.UserId = Guid.NewGuid().ToString();
_groupData.GroupName = "GroupName1";
// The server SignalRMessageAction("joinedGroupMessage") which is invoked via SendToGroup does not trigger this
_connection.On<string>("joinedGroupMessage", (groupMessage) => { chatMessages.Text = groupMessage; });
// This successfully invokes the server side Negotiate function
Task.Run(() =>
{
Dispatcher.Dispatch(async () =>
await _connection.StartAsync());
});
}
private async void AddToGroupClicked(object sender, EventArgs e)
{
// This successfully invokes the server side function succesfully
await _connection.InvokeAsync("AddToGroup", _groupData.GroupName, _groupData.UserId);
}
private async void OnSendClicked(object sender, EventArgs e)
{
// This invokes the server side function succesfully. However when the server returns
// the SignalRMessageAction, the "joinedGroupMessage" (see MainPage() above is not triggered
await _connection.InvokeAsync("SendToGroup", _groupData.GroupName, "message1");
}
}
Again, I have a fully configured local environment, all the functions in the client above are successfully invoking their server side counterparts. The outstanding issue is that server SendToGroup function fails to trigger the _connection.On "joinGroupMessage" event above. This is likely because something is amiss server side wise.