CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Link Preview

Learn how you can show a preview of the web page for links.

The Link Preview extension will help you show a preview of the web page for every link in your message. We generate metadata for every link mentioned in a message.

Say, for example, a user shares a Facebook link in their message, then our extension will query the link for the details that you need to build a preview.

"@injected": {
  "extensions": {
    "link-preview": {
      "links": [
        {
          "description": "Create an account or log into Facebook. Connect with friends, family and other people you know. Share photos and videos, send messages and get updates.",
          "favicon": "https://static.xx.fbcdn.net/rsrc.php/yz/r/KFyVIAWzntM.ico",
          "image": "https://www.facebook.com/images/fb_icon_325x325.png",
          "title": "Facebook - Log In or Sign Up",
          "url": "https://www.facebook.com"
        }
      ]
    }
  }
}

Using this metadata, you can build a preview box similar to the one you seen in Slack.

Use CometChat's Link Preview Extension to build a preview box.

Use CometChat's Link Preview Extension to build a preview box.

At the recipients' end, in the message object, you can fetch the metadata by calling the getMetadata() method. Using this metadata, you can fetch the URL information to generate the preview.

JSONObject metadata = message.getMetadata();
        if (metadata != null) {
        JSONObject injectedObject = metadata.getJSONObject("@injected");
        if (injectedObject != null && injectedObject.has("extensions")) {
        JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
        if (extensionsObject != null && extensionsObject.has("link-preview")){
          
         JSONObject linkObject = extensionsObject.getJSONObject("link-preview");
         JSONArray linkArray= linkObject.getJSONArray("links");
         JSONObject linkPreviewObject=linkArray.getJSONObject(0);
          if (linkPreviewObject.has("description"))
         String description = linkPreviewObject.getString("description");
          if (linkPreviewObject.has("favicon"))
         String favicon = linkPreviewObject.getString("favicon");
          if (linkPreviewObject.has("image"))
         String image = linkPreviewObject.getString("image");
          if (linkPreviewObject.has("title"))
         String title = linkPreviewObject.getString("title");
          if (linkPreviewObject.has("url"))
         String url = linkPreviewObject.getString("url");

     }
   }
 }
if (metadata != null) {
   if (metadata.has("@injected")) {
   val injectedJSONObject = metadata.getJSONObject("@injected")
   if (injectedJSONObject != null && injectedJSONObject.has("extensions")) {
   val extensionsObject = injectedJSONObject.getJSONObject("extensions")
   if (extensionsObject.has("link-preview")) {

       val linkObject = extensionsObject.getJSONObject("link-preview")
       val linksArray = linkObject.getJSONArray("links")
       val linkPreviewObject = linksArray.getJSONObject(0)

       if (linkPreviewObject.has("description")) 
          val description = linkPreviewObject.getString("description")
       if (linkPreviewObject.has("favicon")) 
           val favicon = linkPreviewObject.getString("favicon")
       if (linkPreviewObject.has("image")) 
           val image= linkPreviewObject.getString("image")
       if (linkPreviewObject.has("title")) 
           val title= linkPreviewObject.getString("title")
       if (linkPreviewObject.has("url"))
           val url=  linkPreviewObject.getString("url")                      
        }
      }
   }
 }

Links that take more than a second to resolve will be automatically skipped to keep in-flight transit time to a minimum.

Link Preview


Learn how you can show a preview of the web page for links.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.