使用 Azure Functions 和 Azure SignalR 服务生成实时应用Build real-time Apps with Azure Functions and Azure SignalR Service

Azure SignalR Service 和 Azure Functions 都是高度可缩放的完全托管服务,使你可以专注于构建应用程序而不是管理基础结构,因此通常将这两种服务结合使用以在无服务器环境中提供实时通信。Because Azure SignalR Service and Azure Functions are both fully managed, highly scalable services that allow you to focus on building applications instead of managing infrastructure, it's common to use the two services together to provide real-time communications in a serverless environment.

将实时通信与 Azure 服务集成在一起Integrate real-time communications with Azure services

Azure Functions 允许你以 JavaScript、Python、C# 和 Java 等多种语言编写代码,在云中发生事件时触发。Azure Functions allow you to write code in several languages, including JavaScript, Python, C#, and Java, that triggers whenever events occur in the cloud. 这些事件的示例包括:Examples of these events include:

  • HTTP 和 Webhook 请求HTTP and webhook requests
  • 周期性定时器Periodic timers
  • Azure 服务中的事件,例如:Events from Azure services, such as:
    • 事件中心Event Hubs
    • 服务总线Service Bus
    • Cosmos DB 更改源Cosmos DB change feed
    • 存储 - blob 和队列Storage - blobs and queues
    • 逻辑应用连接器,例如 Salesforce 和 SQL ServerLogic Apps connectors such as Salesforce and SQL Server

通过使用 Azure Functions 将这些事件与 Azure SignalR Service相集成,可在事件发生时通知数以千计的客户端。By using Azure Functions to integrate these events with Azure SignalR Service, you have the ability to notify thousands of clients whenever events occur.

可以使用 Azure Functions 和 SignalR Service 实现的实时无服务器消息传递的一些常见情况包括:Some common scenarios for real-time serverless messaging that you can implement with Azure Functions and SignalR Service include:

  • 在实时仪表板或地图上可视化 IoT 设备遥测Visualize IoT device telemetry on a real-time dashboard or map
  • 在 Cosmos DB 中更新文档时更新应用程序中的数据Update data in an application when documents update in Cosmos DB
  • 在 Salesforce 中创建新订单时发送应用内通知Send in-app notifications when new orders are created in Salesforce

Azure Functions 的 SignalR Service 绑定SignalR Service bindings for Azure Functions

通过 Azure Functions 的 SignalR Service 绑定,可以使用 Azure Function 应用向连接到 SignalR Service 的客户端发布消息。The SignalR Service bindings for Azure Functions allow an Azure Function app to publish messages to clients connected to SignalR Service. 客户端可以使用 .NET、JavaScript 和 Java 中可用的 SignalR 客户端 SDK 连接到服务,即将推出更多语言。Clients can connect to the service using a SignalR client SDK that is available in .NET, JavaScript, and Java, with more languages coming soon.

示例方案An example scenario

使用 Azure Functions 将 Azure Cosmos DB 和 SignalR Service 集成在一起以便在 Cosmos DB 更改源上出现新事件时发送实时消息,这就是使用 SignalR Service 绑定的一个示例。An example of how to use the SignalR Service bindings is using Azure Functions to integrate with Azure Cosmos DB and SignalR Service to send real-time messages when new events appear on a Cosmos DB change feed.

Cosmos DB, Azure Functions, SignalR Service

  1. 在 Cosmos DB 集合中进行了更改A change is made in a Cosmos DB collection
  2. 更改事件传播到 Cosmos DB 更改源The change event is propagated to the Cosmos DB change feed
  3. 通过使用 Cosmos DB 触发器的更改事件触发 Azure FunctionsAn Azure Functions is triggered by the change event using the Cosmos DB trigger
  4. SignalR Service 输出绑定将消息发布到 SignalR ServiceThe SignalR Service output binding publishes a message to SignalR Service
  5. SignalR Service 将消息发布到所有连接的客户端SignalR Service publishes the message to all connected clients

身份验证和用户Authentication and users

使用 SignalR Service 可以向所有客户端或仅向客户端子集(例如属于单个用户的客户端)广播消息。SignalR Service allows you to broadcast messages to all clients or only to a subset of clients, such as those belonging to a single user. Azure Functions 的 SignalR 服务绑定可以与应用服务身份验证结合,以使用 Azure Active Directory 和 Microsoft 帐户等提供程序对用户进行身份验证。The SignalR Service bindings for Azure Functions can be combined with App Service Authentication to authenticate users with providers such as Azure Active Directory and Microsoft Account. 然后,你可以直接向这些经过身份验证的用户发送消息。You can then send messages directly to these authenticated users.

后续步骤Next steps

在本文中,你将大致了解如何结合使用 Azure Functions 和 SignalR Service 实现各种无服务器实时消息传递的情景。In this article, you got an overview of how to use Azure Functions with SignalR Service to enable a wide array of serverless real-time messaging scenarios.

有关如何使用 Azure Functions 和 SignalR 服务的完整详细信息,请访问以下资源:For full details on how to use Azure Functions and SignalR Service together visit the following resources:

请按照以下其中一个快速入门了解详细信息。Follow one of these quickstarts to learn more.