Skip to main content
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, links, 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
referenceIdStringID of the content being commented on
referenceTypeEnumType of content (post, story, content)
textStringComment text content (max 20,000 characters)
parentIdStringID of parent comment for threaded replies (see reply hierarchy)
metadataObjectCustom metadata for the comment
linksArray<Object>Links detected in text. Each item follows: { url, renderPreview, index?, length?, domain?, title?, imageUrl? }.
attachmentsArray<Object>Files to attach to the comment. Each item is an object: [{ type: "image", fileId: "file_id_1" }, { type: "image", fileId: "file_id_2" }]
mentioneesArray<Object>Users to mention in the comment. Each item is an object: [{ type: "user", userIds: ["userId1","userId2"] }]
links is optional. If omitted, SDKs do not send the links field. For update APIs, passing an empty array clears links while nil or null keeps existing links unchanged.
Use the platform-specific links API to attach detected URLs to a text comment.
let createOptions = AmityCommentCreateOptions(
    referenceId: "post-id",
    referenceType: .post,
    text: "Check this out https://www.amity.co",
    links: [
        AmityLink(
            url: "https://www.amity.co",
            index: 15,
            length: 20,
            renderPreview: false,
            domain: "www.amity.co"
        )
    ]
)

let comment = try await commentRepository.createComment(with: createOptions)
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
    }
}

Creating Replies (Multi-Level)

The social.plus comment system supports replies up to Level 2. The way you pass parentId differs depending on which level the target comment is at:
The third path (replying to an L2 comment) applies only if you are building a UI that displays L2 replies with a Reply action. The UIKit handles this automatically by resolving to the L1 ancestor. If you are using the SDK directly, Level 2 is the maximum supported depth — avoid creating replies beyond it.
ScenarioparentId ValueResult
Reply to L0 commentThe L0 commentIdCreates an L1 reply
Reply to L1 replyThe L1 commentIdCreates an L2 reply
The system supports up to Level 2 only. While the SDK does not prevent you from passing an L2 comment’s ID as parentId (which would technically create an L3 reply), doing so is not supported. System features such as childrenCount, reply aggregation, and notifications only work up to Level 2. Any replies beyond Level 2 will not be counted or displayed correctly.If you are building a custom UI that allows replying to L2 comments, you should resolve the parentId to the L1 ancestor using the target comment’s getParentId() method, so the new reply is created as another L2 comment.
@Mention Pre-fill: When replying to L1 or L2 replies, the compose bar typically pre-fills the text input with @[author-name] of the comment being replied to, making it clear who the reply is directed at.

Best Practices

  • Immediate Feedback: Show comments instantly in the UI for better user experience
  • Rollback Strategy: Implement proper error handling to remove failed comments
  • Loading States: Show appropriate indicators during comment submission
  • Network Resilience: Handle offline scenarios with queued comment creation
  • Text Validation: Validate comment length before API calls
  • Debounce Input: Prevent rapid successive submissions
  • Memory Management: Properly dispose of notification tokens
  • Background Processing: Handle comment creation asynchronously
  • Character Limits: Show remaining character count to users
  • Auto-save Drafts: Save comment drafts as users type
  • Keyboard Management: Handle keyboard appearance for better UX
  • Visual Feedback: Provide clear success/error indicators
  • Moderation: Implement client-side content filtering if needed
  • Rich Text: Support basic text formatting where appropriate
  • Mention Handling: Provide user-friendly mention input interfaces
  • Thread Management: Display reply hierarchies clearly

Troubleshooting

  • Verify user has permission to comment on the content
  • Check if the reference ID exists and is accessible
  • Ensure comment text doesn’t exceed 20,000 character limit
  • Validate network connectivity and authentication status
  • Confirm UI update logic is executed on main thread
  • Check if error handling properly removes failed comments
  • Test with different network conditions
  • Ensure parentId is valid and references existing comment
  • Check comment hierarchy depth limits
  • Verify UI properly handles nested comment display
  • Test reply functionality with different comment types
  • Implement proper pagination for comment lists
  • Optimize comment rendering for large threads
  • Monitor memory usage with extensive comment trees
  • Use efficient data structures for comment hierarchies

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.

Comment Retrieval

Learn how to query and retrieve comments from posts and stories

Comment Actions

Explore comment editing and deletion functionality

Comment Engagement

Discover how to implement reactions and interactions on comments

Mentions

Understand how to implement user mentions in comments