Skip to content

Repository files navigation

Note:getstream-net is the new full-product SDK for Stream, covering Chat, Video, Moderation, and Feeds with long-term support.

stream-chat-net is now in maintenance mode. It will continue to receive critical fixes and requested features, so it is safe to keep using. However, we encourage existing users to migrate when convenient and strongly recommend that new projects start with getstream-net.

Check out the Migration Guide for side-by-side examples covering common use cases.

Official .NET SDK for Stream Chat

.github/workflows/ci.yamlNuGet Badge

Official .NET API client for Stream Chat, a service for building chat applications.
Explore the docs »

Code Samples · Report Bug · Request Feature

📝 About Stream

You can sign up for a Stream account at our Get Started page.

You can use this library to access chat API endpoints server-side.

For the client-side integrations (web and mobile) have a look at the JavaScript, iOS and Android SDK libraries (docs).


🚨 Breaking changes in v1.0 <

The library received many changes in v1.0 to make it easier to use and more maintanable in the future. The main change is that both Channel and Client classes have been separated into small modules that we call clients. (This resambles the structure of our Java library as well.) Main changes:

  • Channel and Client classes are gone, and have been organized into smaller clients in StreamChat.Clients namespace.
  • These clients do not maintain state as Channel used to did earlier where it kept the channelType and channelId in the memory. So this means that you'll need to pass in channelType and channelId to a lot of method calls in IChannelClient.
  • Async method names have Async suffix now.
  • All public methods and classes have documentation.
  • Identifiers has been renamed from ID to Id to follow Microsoft's naming guide. Such as userID -> userId.
  • A lot of data classes have been renamed to make more sense. Such as ChannelObject -> Channel.
  • Data classes have been moved to StreamChat.Models namespace.
  • Full feature parity: all backend APIs are available.
  • Returned values are type of ApiResponse and expose rate limit informaiton with GetRateLimit() method.
  • The folder structure of the project has been reorganized to follow Microsoft's recommendation.
  • Unit tests have been improved. They are smaller, more focused and have cleanup methods.
  • Added .NET 6.0 support.

The proper usage of the library:

varclientFactory=newStreamClientFactory("YourApiKey","YourApiSecret");// Note: all client instances can be used as a singleton for the lifetime// of your application as they don't maintain state.varuserClient=clientFactory.GetUserClient();varchannelClient=clientFactory.GetChannelClient();varmessageClient=clientFactory.GetMessageClient();varreactionClient=clientFactory.GetReactionClient();varjamesBond=awaituserClient.UpsertAsync(newUserRequest{Id="james_bond"});varagentM=awaituserClient.UpsertAsync(newUserRequest{Id="agent_m"});varchannel=awaitchannelClient.GetOrCreateAsync("messaging","superHeroChannel",createdBy:jamesBond.Id);awaitchannelClient.AddMembersAsync(channel.Type,channel.Id,jamesBond.Id,agentM.Id);varmessage=awaitmessageClient.SendMessageAsync(channel.Type,channel.Id,jamesBond.Id,"I need a new quest Agent M.");awaitreactionClient.SendReactionAsync(message.Id,"like",agentM.Id);

⚙️ Installation

$ dotnet add package stream-chat-net

💡 Tip: you can find code samples in the samples folder.

✨ Getting started

Import

usingStreamChat.Clients;

Initialize client

// Client factory instantiation.varclientFactory=newStreamClientFactory("YourApiKey","YourApiSecret");// Or you can configure some options such as custom HttpClient, HTTP timeouts etc.varclientFactory=newStreamClientFactory("YourApiKey","YourApiSecret", opts =>opts.Timeout=TimeSpan.FromSeconds(5));// Get clients from client factory. Note: all clients can be used as a singleton in your application.varchannelClient=clientFactory.GetChannelClient();varmessageClient=clientFactory.GetMessageClient();

Generate a token for client-side usage

varuserClient=clientFactory.GetUserClient();// Without expirationvartoken=userClient.CreateToken("bob-1");// With expirationvartoken=userClient.CreateToken("bob-1",expiration:DateTimeOffset.UtcNow.AddHours(1));

Create/Update users

varuserClient=clientFactory.GetUserClient();varbob=newUserRequest{Id="bob-1",Role=Role.Admin,Teams=new[]{"red","blue"}// if multi-tenant enabled};bob.SetData("age",27);awaituserClient.UpsertAsync(bob);// Batch update is also supportedvarjane=newUserRequest{Id="jane"};varjune=newUserRequest{Id="june"};varusers=awaituserClient.UpsertManyAsync(new[]{bob,jane,june});

GDPR-like User endpoints

varuserClient=clientFactory.GetUserClient();awaituserClient.ExportAsync("bob-1");awaituserClient.DeactivateAsync("bob-1");awaituserClient.ReactivateAsync("bob-1");awaituserClient.DeleteAsync("bob-1");

Channel types

varchannelTypeClient=clientFactory.GetChannelTypeClient();varchanTypeConf=newChannelTypeWithStringCommands{Name="livechat",Automod=Automod.Disabled,Commands=newList<string>{Commands.Ban},Mutes=true};varchanType=awaitchannelTypeClient.CreateChannelTypeAsync(chanTypeConf);varallChanTypes=awaitchannelTypeClient.ListChannelTypesAsync();

Channels

varchannelClient=clientFactory.GetChannelClient();// Create a channel with members from the start, Bob is the creatorvarchannel=channelClient.GetOrCreateAsync("messaging","bob-and-jane",bob.Id,bob.Id,jane.Id);// Create channel and then add members, Mike is the creatorvarchannel=channelClient.GetOrCreateAsync("messaging","bob-and-jane",mike.Id);channelClient.AddMembersAsync(channel.Type,channel.Id,bob.Id,jane.Id,joe.Id);

Messaging

varmessageClient=clientFactory.GetMessageClient();// Only textmessageClient.SendMessageAsync(channel.Type,channel.Id,bob.Id,"Hey, I'm Bob!");// With custom datavarmsgReq=newMessageRequest{Text="Hi june!"};msgReq.SetData("location","amsterdam");varbobMessageResp=awaitmessageClient.SendMessageAsync(channelType,channel.Id,msgReq,bob.Id);// ThreadsvarjuneReply=newMessageRequest{Text="Long time no see!"};varjuneReplyMessage=awaitmessageClient.SendMessageToThreadAsync(channel.Type,channel.Id,juneReply,june.Id,bobMessageResp.Message.Id)

Reactions

varreactionClient=clientFactory.GetReactionClient();awaitreactionClient.SendReactionAsync(message.Id,"like",bob.Id);varallReactions=awaitreactionClient.GetReactionsAsync(message.Id);

Moderation

varchannelClient=clientFactory.GetChannelClient();varuserClient=clientFactory.GetUserClient();varflagClient=clientFactory.GetFlagClient();awaitchannelClient.AddModeratorsAsync(channel.Type,channel.Id,new[]{jane.Id});awaituserClient.BanAsync(newBanRequest{Type=channel.Type,Id=channel.Id,Reason="reason",TargetUserId=bob.Id,UserId=jane.Id});awaitflagClient.FlagUserAsync(bob.Id,jane.Id);

Permissions

varpermissionClient=clientFactory.GetPermissionClient();awaitpermissionClient.CreateRoleAsync("channel-boss");// Assign users to roles (optional message)awaitchannelClient.AssignRolesAsync(newAssignRoleRequest{AssignRoles=newList<RoleAssignment>{newRoleAssignment{UserId=bob.ID,ChannelRole=Role.ChannelModerator},newRoleAssignment{UserId=june.ID,ChannelRole="channel-boss"}},Message=newMessageRequest{Text="Bob and June just became mods",User=bob}});

Devices

vardeviceClient=clientFactory.GetDeviceClient();varjunePhone=newDevice{ID="iOS Device Token",PushProvider=PushProvider.APN,UserId=june.ID};awaitdeviceClient.AddDeviceAsync(junePhone);vardevices=awaitdeviceClient.GetDevicesAsync(june.Id);

Export Channels

varchannelClient=clientFactory.GetChannelClient();vartaskClient=clientFactory.GetTaskClient();vartaskResponse=channelClient.ExportChannelAsync(newExportChannelRequest{Id=channel.Id,Type=channel.Type});// Wait for the completionvarcomplete=false;variterations=0;AsyncTaskStatusResponseresp=null;while(!complete&&iterations<1000){resp=awaittaskClient.GetTaskStatusAsync(taskResponse.TaskId);if(resp.Status==AsyncTaskStatus.Completed){complete=true;break;}iterations++;awaitTask.Delay(100);}if(complete){Console.WriteLine(resp.Result["url"]);}

✍️ Contributing

We welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our Contributor License Agreement (CLA) first. See our license file for more details.

Head over to CONTRIBUTING.md for some development tips.

🧑‍💻 We are hiring!

We've recently closed a $38 million Series B funding round and we keep actively growing. Our APIs are used by more than a billion end-users, and you'll have a chance to make a huge impact on the product within a team of the strongest engineers all over the world.

Check out our current openings and apply via Stream's website.

About

Official Stream Chat API client for .NET (maintenance mode). For new projects, see getstream-net.

Topics

Resources

Contributing

Security policy

Stars

75 stars

Watchers

31 watching

Forks

Releases

Packages

Used by

Contributors

Languages