[MS] Building Real‑Time iOS Apps with SignalR: Introducing the Official Swift Client (Public Preview) - devamazonaws.blogspot.com
Introduction
Until now, iOS developers who wanted real‑time, bi‑directional communication with SignalR had to rely on community‑built clients or roll their own Swift implementation—both of which introduced maintenance and compatibility headaches. We’re excited to announce that the official SignalR Swift client is now available in public preview.
With this release, you can:
- Quickly add real‑time features (chat, notifications, live dashboards) to your SwiftUI or UIKit apps.
- Leverage full SignalR functionality—hubs, groups, client/server streaming—on iOS and macOS.
- Rely on an officially supported, maintained, and tested library.
How to set up Swift client and use its core features
Requirements
- Swift >= 5.10
- macOS >= 11.0
- iOS >= 14
Install via Swift Package Manager
Add the SignalR Swift package as a dependency in yourPackage.swift file:
After adding the dependency, import the library in your Swift code: Establish a Hub Connection Create and start your connection:// swift-tools-version: 5.10import PackageDescriptionlet package = Package(name: "signalr-client-app",dependencies: [.package(url: "https://github.com/dotnet/signalr-client-swift", branch: "main")],targets: [.executableTarget(name: "YourTargetName", dependencies: [.product(name: "SignalRClient", package: "signalr-client-swift")])])
Tip: Register your handlers before callinglet connection = HubConnectionBuilder().withUrl(url: "https://your-signalr-server/hub").build()try await connection.start()
start() to avoid missing early messages. After adding the dependency, import the library in your Swift code:
import SignalRClient
Receive Server Calls
To handle messages from the server, use on:
await connection.on("ReceiveMessage") { (user: String, msg: String) inprint("\(user): \(msg)") }
On your .NET hub:
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message) =>
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
The method name and parameters must match exactly.
Post Updated on April 23, 2025 at 04:46AM
Thanks for reading
from devamazonaws.blogspot.com
Comments
Post a Comment