How do you make a bare-bones Serverless SingnalR app? The docs on this are really scattered, and it seems like all the trad. asp versions keep saying, "Dont worry about this stuff," which is required for serverless.
This works for Negotiation, but what am I missing after that? The calls to the api do not hit endpoints. I am only running this locally. This app does work as a server responding to http requests, without the SignalR stuff, so I can hit some endpoints.
Backend code:
// This gets hit and succeeds:
[FunctionName("negotiate")]
public static SignalRConnectionInfo Negotiate(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
[SignalRConnectionInfo(HubName = "TestHub")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
// This works in an ASP app, but doesn't get hit in Serverless:
public async Task SendMessage(string message)
{
char[] stringArray = message.ToCharArray();
Array.Reverse(stringArray);
string reversedStr = new string(stringArray);
await Clients.All.SendAsync("ReceiveMessage", reversedStr);
}
Frontend code:
// All this works for Vue talking to the ASP app, and it hits Negotiate with serverless, but noting else:
const connection = new signalR.HubConnectionBuilder()
//.withUrl("http://localhost:5192/chatHub") //SignalR-POC.ASP
.withUrl("http://localhost:7230/api") //Serverless running locally
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect()
.build();
// Inbound: `connection.on()`s should be registered before `.start` executes
connection.on("ReceiveMessage", (reply) => {
console.log("[SignalR.ReceiveMessage] " + reply);
serverReply.value = reply;
});
async function start() {
try {
await connection.start();
console.log("[SignalR.start] Connected.");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
// Outbound
async function sendToServer(){
try {
console.log("[SignalR.SendMessage] " + userInput.value);
await connection.invoke("SendMessage", userInput.value);
} catch (err) {
console.error(err);
}
}
connection.onclose( async () => {
console.log("[SignalR.onclose] Closed.");
await start()
});
// Start the connection.
start();
</script>
<style>
.table2x3{
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(3, 1fr);
}
.my-button{
grid-column-end: span 2;
margin-top:0.5em;
}
Here is the way i am able to run Serverless SingnalR api & client locally and able to hit the endpoint
1. Serverless SignalR API:
Azure Functionsand create the project.HTTP triggerand provide a name (e.g., "HttpFunction").Here is the function code
2. Serverless SignalR Hub: Create a new SignalR hub by adding a new class (e.g.,
ChatHub.cs) to your project:3.Configure SignalR Connection String:
4.Client:
5. Run the Solution Function Triggered
Result Using Postman Select Post,Paste your api and Click Send
Pass your values
Result