Migration Guide to 5.x

This guide will help you migrate your code if upgrading the Unity Chat SDK to the 5.x version.

The primary change was to replace enums with custom structs. This change makes the SDK much more resilient to API changes, but it may require you to refactor parts of your code that rely on the enums’ compilation constant property.

Affected Types

Here’s a full list of enum types that got replaced with a struct:

Namespace -> Type

  • StreamChat.Core.Models - > StreamMessageType

  • StreamChat.Core.Responses -> StreamImageCropType

  • StreamChat.Core.Responses -> StreamImageResizeType

  • StreamChat.Core.InternalDTO.Models -> AutomodBehaviourType

  • StreamChat.Core.InternalDTO.Models -> AutomodType

  • StreamChat.Core.InternalDTO.Models -> BlockListOptionsBehavior

  • StreamChat.Core.InternalDTO.Models -> ImageCropType

  • StreamChat.Core.InternalDTO.Models -> ImageResizeType

  • StreamChat.Core.InternalDTO.Models -> MessageType

  • StreamChat.Core.InternalDTO.Models -> PushProviderType

  • StreamChat.Core.InternalDTO.Requests -> CreateCallRequestType

  • StreamChat.Core.InternalDTO.Requests -> CreatePollRequestVotingVisibility

  • StreamChat.Core.InternalDTO.Requests -> MessageRequestType

  • StreamChat.Core.InternalDTO.Requests -> TranslateMessageRequestLanguage

  • StreamChat.Core.InternalDTO.Requests -> UpdatePollRequestVotingVisibility

  • StreamChat.Core.InternalDTO.Responses -> ChannelMemberResponseRole

Non-breaking Changes

In many cases, the migration from enums to structs will not require any code changes at all, as the syntax remains the same.

Variable Declaration and Assignment

// Old (enum)
StreamMessageType messageType = StreamMessageType.Regular;

// New (struct)
StreamMessageType messageType = StreamMessageType.Regular;

Method Parameters and Return Types

// Old (enum)
public void ProcessMessage(StreamMessageType type) { ... }

// New (struct)
public void ProcessMessage(StreamMessageType type) { ... }

Basic Equality Comparisons

// Old (enum)
if (messageType == StreamMessageType.Regular) { ... }

// New (struct)
if (messageType == StreamMessageType.Regular) { ... }

Breaking Changes and How to Fix Them

While many use cases will work the same, there are some scenarios where the change from enums to structs will require code modifications.

Switch Statements

The most common scenario requiring rewriting the code is when an enum is used in a switch statement. Such cases must be converted to a chain of if/else statements or use the latest pattern-matching syntax in C# 7.0+.

// Old (enum)
switch (messageType)
{
  case StreamMessageType.Regular:
    // Handle regular message
    break;
  case StreamMessageType.System:
    // Handle system message
    break;
  // ...
}

// New (struct) - Using if-else statements
if (messageType == StreamMessageType.Regular)
{
  // Handle regular message
}
else if (messageType == StreamMessageType.System)
{
  // Handle system message
}
// ...

// Alternative: Using pattern matching (C# 7.0+)
switch (messageType)
{
  case var type when type == StreamMessageType.Regular:
    // Handle regular message
    break;
  case var type when type == StreamMessageType.System:
    // Handle system message
    break;
  // ...
}

Initialization and Default Values

Enums have an implicit default value of 0. Structs do not, and you may need to explicitly initialize the struct where an enum default was previously used.

Old code (with enum):

StreamMessageType messageType = default; // Implicitly StreamMessageType.Regular

New code (with struct):

StreamMessageType messageType = StreamMessageType.Regular; // Explicit initialization needed

Const assignment

Enums were allowed to be used in compile-time constants.

Old code (with enum):

public const StreamMessageType DefaultMessageType = StreamMessageType.Regular;

New code (with struct):

public static readonly StreamMessageType DefaultMessageType = StreamMessageType.Regular;

Structs cannot be const because they are reference types, so you cannot assign them as compile-time constants. Instead, the closest alternative is to use static readonly. This introduces a slight difference: while const values are replaced at compile-time, static readonly values are evaluated at runtime. This means that code relying on const behavior (e.g., embedded in assemblies or libraries) might require adjustment to work with the new static readonly fields.

Summary

This concludes the migration guidance on upgrading your project to Stream Chat SDK for Unity 5.x. If you’ve encountered any issues not mentioned in this guide or have any questions about the migration process, please don’t hesitate to contact us. We’re here to assist you and ensure a smooth transition to your projects’ latest version of the SDK.

© Getstream.io, Inc. All Rights Reserved.