Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Specify files that shouldn't be modified by Fern
README.md

scripts/
examples/
src/SchematicHQ.Client.Test/Datastream/
src/SchematicHQ.Client.Test/TestCache.cs
src/SchematicHQ.Client.Test/TestClient.cs
src/SchematicHQ.Client.Test/TestCompaniesClient.cs
src/SchematicHQ.Client.Test/TestEventBuffer.cs
src/SchematicHQ.Client.Test/TestLogger.cs
src/SchematicHQ.Client.Test/Webhooks/
src/SchematicHQ.Client/Cache.cs
src/SchematicHQ.Client/Cache/
src/SchematicHQ.Client/Core/Public/ClientOptionsCustom.cs
src/SchematicHQ.Client/Datastream
src/SchematicHQ.Client/EventBuffer.cs
src/SchematicHQ.Client/Logger.cs
src/SchematicHQ.Client/Schematic.cs
src/SchematicHQ.Client/Webhooks/WebhookUtils/
src/SchematicHQ.RulesEngine
18 changes: 12 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ jobs:
- uses: actions/checkout@master

- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
dotnet-version: |
8.0.x
9.0.x

- name: Install tools
run: |
Expand All @@ -34,9 +36,11 @@ jobs:
- uses: actions/checkout@master

- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
dotnet-version: |
8.0.x
9.0.x

- name: Install tools
run: |
Expand All @@ -57,9 +61,11 @@ jobs:
uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
dotnet-version: |
8.0.x
9.0.x

- name: Publish
env:
Expand Down
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,126 @@ try {
}
```

## Datastream

Datastream is Schematic's real-time connection service that optimizes flag check performance and reliability. When enabled, the Schematic client maintains a WebSocket connection to our servers, which pushes down feature flag definitions, company data, and user data as needed.

### Benefits of Datastream

- **Improved Performance**: Flag checks become near-instantaneous after the initial data load
- **Reduced API Load**: Minimizes HTTP requests to the Schematic API
- **Real-time Updates**: Flag changes are immediately pushed to your application
- **Fault Tolerance**: Falls back to standard API requests when needed

### Enabling Datastream

**Important**: Datastream is disabled by default. You must explicitly enable it in your client options:

```csharp
using SchematicHQ.Client;

// Create options with Datastream enabled
var options = new ClientOptions
{
UseDatastream = true // Enable Datastream
};

// Initialize client with options
var schematic = new Schematic("YOUR_API_KEY", options);
```

### Customizing Datastream

You can customize Datastream's behavior through additional client options:

```csharp
using SchematicHQ.Client;
using SchematicHQ.Client.Cache;
using SchematicHQ.Client.Datastream;

// Create options with Datastream configuration
var options = new ClientOptions
{
// Enable Datastream (required)
UseDatastream = true,

// Configure Datastream-specific options
DatastreamOptions = new DatastreamOptions()
// Use Redis cache for better performance in distributed environments
.WithRedisCache(
connectionStrings: new List<string>
{
"redis-primary.example.com:6379",
"redis-replica.example.com:6379"
},
keyPrefix: "my-app:",
cacheTtl: TimeSpan.FromMinutes(10),
database: 0
)
// Or use local cache for single-instance applications
//.WithLocalCache(capacity: 10000, ttl: TimeSpan.FromMinutes(5))
};

// Initialize client with options
var schematic = new Schematic("YOUR_API_KEY", options);
```

### Using Datastream with Shared Cache Configuration

You can configure your main client cache and Datastream to use the same cache settings:

```csharp
using SchematicHQ.Client;
using SchematicHQ.Client.Cache;

// Configure with multiple Redis endpoints for high availability
var options = new ClientOptions
{
// Enable Datastream
UseDatastream = true,

// Create cache configuration with Redis settings (used by both main client and Datastream)
CacheConfiguration = new CacheConfiguration
{
ProviderType = CacheProviderType.Redis,
RedisConnectionStrings = new List<string>
{
"redis-1.example.com:6379",
"redis-2.example.com:6379",
"redis-3.example.com:6379"
},
RedisKeyPrefix = "prod:",
CacheTtl = TimeSpan.FromMinutes(10)
}
};

// Schematic will use the same cache configuration for both
// the main client and Datastream client
var schematic = new Schematic("YOUR_API_KEY", options);
```

### Checking Flags with Datastream

The flag checking experience remains the same whether Datastream is enabled or not:

```csharp
// Check a feature flag with Datastream enabled
bool flagValue = await schematic.CheckFlag(
"premium-feature",
company: new Dictionary<string, string> { { "id", "company-123" } },
user: new Dictionary<string, string> { { "email", "user@example.com" } }
);

// Use the flag result
if (flagValue) {
// Enable premium feature
} else {
// Use standard feature
}
```

The difference is that with Datastream enabled, after the initial data load, subsequent flag checks for the same company and user will be nearly instantaneous and won't require additional network requests.

## Contributing
While we value open-source contributions to this SDK, this library
is generated programmatically. Additions made directly to this library
Expand Down
17 changes: 17 additions & 0 deletions examples/DatastreamTestServer/DatastreamTestServer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../src/SchematicHQ.Client/SchematicHQ.Client.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions examples/DatastreamTestServer/DatastreamTestServer.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@DatastreamTestServer_HostAddress = http://localhost:5053

GET {{DatastreamTestServer_HostAddress}}/weatherforecast/
Accept: application/json

###
65 changes: 65 additions & 0 deletions examples/DatastreamTestServer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.Extensions.Options;
using SchematicHQ.Client;
using SchematicHQ.Client.Cache;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

string apiKey = Environment.GetEnvironmentVariable("SCHEMATIC_API_KEY") ??
throw new InvalidOperationException("SCHEMATIC_API_KEY environment variable is not set");

var options = new ClientOptions
{
BaseUrl = "http://localhost:8080",
UseDatastream = true,
DatastreamOptions = new SchematicHQ.Client.Datastream.DatastreamOptions
{
CacheTTL = TimeSpan.FromHours(24)
}
};

options.WithRedisCache(
new List<string> { "localhost:6379" }, // Redis connection string
keyPrefix: "schematic-test:", // Optional key prefix
cacheTtl: TimeSpan.FromHours(24)// Optional cache TTL
);

options.WithHttpClient(new HttpClient
{
BaseAddress = new Uri("http://localhost:8080")
});

Schematic schematic = new Schematic(apiKey, options);

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();
app.MapGet("/", () => "Welcome to the Schematic Datastream Test Server!");

app.MapPost("/checkflag", async (CheckFlagRequestBody request) =>
{
var result = await schematic.CheckFlag(request.FlagKey, request.Company, request.User);
return Results.Ok(result);
}).WithName("CheckFlag");

app.Run();

record CheckFlagRequestBody
{
[JsonPropertyName("company")]
public Dictionary<string, string>? Company { get; init; } = null;
[JsonPropertyName("user")]
public Dictionary<string, string>? User { get; init; } = null;
[JsonPropertyName("flag-key")]
public string FlagKey { get; init; } = string.Empty;
};
23 changes: 23 additions & 0 deletions examples/DatastreamTestServer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5053",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7227;http://localhost:5053",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions examples/DatastreamTestServer/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions examples/DatastreamTestServer/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SchematicHQ.WebhookTestServer</RootNamespace>
Expand Down
Loading
Loading