Skip to content

Commit

Permalink
JSInterop Streaming implementation to allow for larger objects over S…
Browse files Browse the repository at this point in the history
…ignalR (#247)

* Added Streaming provider to support storing objects larger than the SignalR message limit

* updated readme to include streaming use

* cleanup

---------

Co-authored-by: Chris Sainty <[email protected]>
  • Loading branch information
CoreyHayward and chrissainty authored Sep 18, 2024
1 parent 8f60bf3 commit e1139f9
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 358 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ builder.Services.AddBlazoredLocalStorageAsSingleton();

This method will not work with Blazor Server applications as Blazor's JS interop services are registered as Scoped and cannot be injected into Singletons.

### Using JS Interop Streaming
When using interactive components in server-side apps JS Interop calls are limited to the configured SignalR message size (default: 32KB).
Therefore when attempting to store or retrieve an object larger than this in LocalStorage the call will fail with a SignalR exception.

The following streaming implementation can be used to remove this limit (you will still be limited by the browser).

Register the streaming local storage service

```c#
public void ConfigureServices(IServiceCollection services)
{
services.AddBlazoredLocalStorageStreaming();
}
```

Add the JavaScript file to your _App.razor_

```html
<script src="_content/Blazored.LocalStorage/Blazored.LocalStorage.js"></script>
```

## Usage (Blazor WebAssembly)
To use Blazored.LocalStorage in Blazor WebAssembly, inject the `ILocalStorageService` per the example below.

Expand Down
1 change: 1 addition & 0 deletions samples/InteractiveServer/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<body>
<Routes @rendermode="InteractiveServer"/>
<script src="_framework/blazor.web.js"></script>
<script src="_content/Blazored.LocalStorage/Blazored.LocalStorage.js"></script>
</body>

</html>
3 changes: 3 additions & 0 deletions samples/InteractiveServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
.AddInteractiveServerComponents();

builder.Services.Replace(ServiceDescriptor.Scoped<IJsonSerializer, NewtonSoftJsonSerializer>());

builder.Services.AddBlazoredLocalStorage();
// Use the below to enable streaming of objects with local storage
//builder.Services.AddBlazoredLocalStorageStreaming();

var app = builder.Build();

Expand Down
4 changes: 4 additions & 0 deletions src/Blazored.LocalStorage/Blazored.LocalStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\Blazored.LocalStorage.js"></Content>
</ItemGroup>

</Project>
Loading

0 comments on commit e1139f9

Please sign in to comment.