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, 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 10,000 characters)
parentIdStringID of parent comment for threaded replies
metadataObjectCustom metadata for the comment
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"] }]
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

  • 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.