social.plus SDK’s comment creation is designed to handle comments efficiently and reliably across your application. Each comment is assigned a unique, immutable commentId, and the SDK includes an optimistic update feature to enhance user experience.

Optimistic Updates

Comments appear instantly with automatic rollback on failure

Threaded Replies

Support nested conversations with parent-child relationships

Rich Content

Text comments with mentions, metadata, and custom data
Optimistic Updates: Comments appear immediately in the UI while the SDK handles creation in the background, providing seamless user experience with automatic rollback on failure.

Reference Types

Comments can be created on different types of content by specifying the appropriate reference type.
Reference TypeDescriptionUse CasesCharacter Limit
postComments on regular postsText posts, media posts, shared content20,000 characters
storyComments on story contentTemporary stories, story highlights20,000 characters
contentComments on specialized contentArticles, custom content types20,000 characters
A comment should not exceed 20,000 characters in length. Comments exceeding this limit will be rejected by the API.

Create Text Comment

To work with comments, you’ll need to use the CommentRepository. With the SDK’s optimistic creation feature, you don’t need to manually create a commentId. Instead, the SDK generates one automatically.

Parameters

ParameterTypeRequiredDescription
referenceIdStringYesID of the content being commented on
referenceTypeEnumYesType of content (.post, .story, .content)
textStringYesComment text content (max 20,000 characters)
parentIdStringNoID of parent comment for threaded replies
var token: AmityNotificationToken?

func createCommentExample() async {
    let createOptions = AmityCommentCreateOptions(referenceId: "post-id", referenceType: .post, text: "comment", parentId: "parent-comment-id")
    do {
        let comment = try await commentRepository.createComment(with: createOptions)
    } catch {
        // Handle error here
    }
}

Reply to a Comment

In addition to creating top-level comments, social.plus SDK enables you to reply to existing comments in addition to creating top-level comments. To reply to a comment, you must:
  • Specify the parent comment’s commentId using the parentId parameter.
  • Ensure the referenceId and referenceType match the original comment’s content.
func replyToCommentWithText(commentRepository: AmityCommentRepository,
                            postId: String,
                            commentId: String) async {
    
    let createOptions = AmityCommentCreateOptions(referenceId: postId,
                                                  referenceType: .post,
                                                  text: "<Comment-Text>",
                                                  parentId: commentId)
   
    do {
       let comment = try await commentRepository.createComment(with: createOptions)
    } catch {
        // Handle error here
    }
}

Best Practices

Troubleshooting

Practical Examples

Social Media Feed

Enable threaded discussions on posts with support for text comments and reactions for comprehensive social engagement.

Customer Support

Create support ticket comments with proper threading for detailed issue reporting and resolution tracking.

Educational Platform

Facilitate course discussions with threaded replies and user mentions for instructor attention and peer interaction.

Community Forums

Build forum-style discussions with nested replies and rich content for knowledge sharing and community building.