title | description | author | ms.author | ms.date | ms.topic | ms.service | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|
Azure SignalR Service serverless quickstart - JavaScript | A quickstart for using Azure SignalR Service and Azure Functions to create an App showing GitHub star count using JavaScript. | vicancy | lianwei | 04/19/2023 | quickstart | azure-signalr-service | javascript | devx-track-js, mode-api |
In this article, you use Azure SignalR Service, Azure Functions, and JavaScript to build a serverless application to broadcast messages to clients.
[!INCLUDE Connection string security]
This quickstart can be run on macOS, Windows, or Linux.
Prerequisite | Description |
---|---|
An Azure subscription | If you don't have a subscription, create an Azure free account |
A code editor | You need a code editor such as Visual Studio Code. |
Azure Functions Core Tools | Requires version 4.0.5611 or higher to run Node.js v4 programming model. |
Node.js LTS | See supported node.js versions in the Azure Functions JavaScript developer guide. |
Azurite | SignalR binding needs Azure Storage. You can use a local storage emulator when a function is running locally. |
Azure CLI | Optionally, you can use the Azure CLI to create an Azure SignalR Service instance. |
[!INCLUDE Create instance]
Make sure you have Azure Functions Core Tools installed.
- Open a command line.
- Create project directory and then change into it.
- Run the Azure Functions
func init
command to initialize a new project.
func init --worker-runtime javascript --language javascript --model V4
After you initialize a project, you need to create functions. This project requires three functions:
index
: Hosts a web page for a client.negotiate
: Allows a client to get an access token.broadcast
: Uses a time trigger to periodically broadcast messages to all clients.
When you run the func new
command from the root directory of the project, the Azure Functions Core Tools creates the function source files storing them in a folder with the function name. You edit the files as necessary replacing the default code with the app code.
Run the following command to create the
index
function.func new -n index -t HttpTrigger
Edit src/functions/httpTrigger.js and replace the contents with the following json code:
:::code language="javascript" source="~/azuresignalr-samples/samples/QuickStartServerless/javascript/v4-programming-model/src/functions/index.js":::
Run the following command to create the
negotiate
function.func new -n negotiate -t HttpTrigger
Edit src/functions/negotiate.js and replace the contents with the following json code:
:::code language="javascript" source="~/azuresignalr-samples/samples/QuickStartServerless/javascript/v4-programming-model/src/functions/negotiate.js":::
Run the following command to create the
broadcast
function.func new -n broadcast -t TimerTrigger
Edit src/functions/broadcast.js and replace the contents with the following code:
:::code language="javascript" source="~/azuresignalr-samples/samples/QuickStartServerless/javascript/v4-programming-model/src/functions/broadcast.js":::
The client interface for this app is a web page. The index
function reads HTML content from the content/index.html file.
Create a folder called
content
in your project root folder.Create the file content/index.html.
Copy the following content to the content/index.html file and save it:
:::code language="html" source="~/azuresignalr-samples/samples/QuickStartServerless/javascript/v4-programming-model/src/content/index.html":::
Azure Functions requires a storage account to work. Choose either of the two following options:
- Run the free Azure Storage Emulator.
- Use the Azure Storage service. This may incur costs if you continue to use it.
Start the Azurite storage emulator:
azurite -l azurite -d azurite\debug.log
Make sure the
AzureWebJobsStorage
in local.settings.json set toUseDevelopmentStorage=true
.
Update the project to use the Azure Blob Storage connection string.
func settings add AzureWebJobsStorage "<storage-connection-string>"
You're almost done now. The last step is to set the SignalR Service connection string in Azure Function app settings.
In the Azure portal, go to the SignalR instance you deployed earlier.
Select Keys to view the connection strings for the SignalR Service instance.
:::image type="content" source="media/signalr-quickstart-azure-functions-javascript/signalr-quickstart-keys.png" alt-text="Screenshot of Azure SignalR service Keys page.":::
Copy the primary connection string, and execute the command.
[!INCLUDE Connection string security comment]
func settings add AzureSignalRConnectionString "<signalr-connection-string>"
Run the Azure Function app in the local environment:
func start
After the Azure Function is running locally, go to http://localhost:7071/api/index
. The page displays the current star count for the GitHub Azure/azure-signalr repository. When you star or unstar the repository in GitHub, you'll see the refreshed count every few seconds.
Having issues? Try the troubleshooting guide or let us know.
[!INCLUDE Cleanup]
You can get all code used in the article from GitHub repository:
In this quickstart, you built and ran a real-time serverless application in localhost. Next, learn more about how to bi-directional communicating between clients and Azure Function with SignalR Service.
[!div class="nextstepaction"] SignalR Service bindings for Azure Functions
[!div class="nextstepaction"] Bi-directional communicating in Serverless
[!div class="nextstepaction"] Deploy Azure Functions with VS Code