Building Real‑Time iOS Apps with SignalR: Introducing the Official Swift Client (Public Preview)

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.

In this post you’ll learn how to set up the Swift client and use its core features. During a recent .NET Community Standup, we demoed an AI-enabled chat sample that uses SignalR for streaming AI‑generated tokens to iOS clients. We recommend you having a watch. You can find the repo referenced in the episode at the end of this blog post.

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 your Package.swift file:

// swift-tools-version: 5.10 
import PackageDescription 
let 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")]) 
        ] 
)

After adding the dependency, import the library in your Swift code:

Establish a Hub Connection

Create and start your connection:

let connection = HubConnectionBuilder()
    .withUrl(url: "https://your-signalr-server/hub")
    .build()

try await connection.start()

Tip: Register your handlers before calling 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) in
      print("(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.

Invoke vs. Send

invoke waits for the server response (and throws on error):

let result: String = try await connection.invoke(
    method: "Calculate", arguments: 42
)

send fires and forgets:

try await connection.send(
    method: "SendMessage", arguments: "Alice", "Hello!"
)

Stream Server Data

For large or incremental data, use stream:

let streamResult: any StreamResult<String> =
    try await connection.stream(method: "StreamNumbers")
    for try await number in streamResult.stream {
        print("Received number: (number)")
    }

On the server you’d implement a streaming hub method:

public async IAsyncEnumerable<int> StreamNumbers()
{   for (var i = 0; i < 10; i++){
        yield return i; await Task.Delay(500);
    }
}

Automatic Reconnect

Enable built‑in retry logic:

let connection = HubConnectionBuilder()
    .withUrl(url: "https://your-signalr-server/hub")
    .withAutomaticReconnect()
    .build()

By default, a client waits 0, 2, 10, and 30 seconds before making reconnect attempts. Customize delays or supply a RetryPolicy for more control. Use onReconnecting or onReconnected to update UI or state.

Logging & Diagnostics

Control log verbosity with:

HubConnectionBuilder()
    .withLogLevel(.debug) // or .information, .warning, .error
    .build()

Transport Configuration

By default, the client prefers WebSockets, then SSE, then long polling. Override this behavior with:

.withUrl(url: "https://…", transport: [.webSockets])

Next Steps

  • Learn more about configuration options available in the Swift client.
  • Explore samples in the signalr-client-swift repo, including the AI chat sample we mentioned earlier.
  • Connect to Azure SignalR Service or your own SignalR server

We’re eager to hear your feedback—open an issue or PR on GitHub and help us refine the Swift client as we move toward GA.

The post Building Real‑Time iOS Apps with SignalR: Introducing the Official Swift Client (Public Preview) appeared first on .NET Blog.

Previous Article

3D modeling you can feel

Next Article

“Periodic table of machine learning” could fuel AI discovery

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *