roMessagePort: Trying to construct a message port on a non-plugin thread

I am getting error in Home.brs , when I calling the service .Getting error at the line  request.SetMessagePort(port)

Here is the error,

BRIGHTSCRIPT: ERROR: roUrlTransfer: creating MAIN|TASK-only component failed on RENDER thread: pkg:/components/Home/Home.brs(67)
BRIGHTSCRIPT: ERROR: roMessagePort: Trying to construct a message port on a non-plugin thread: pkg:/components/Home/Home.brs(68)

BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Suspending threads…
Thread selected:  1*   pkg:/components/Home/Home.brs(69)       request.SetMessagePort(port)

Current Function:
065:  Function getMenuItemsByGroup() 
066:      mplinkStatus = CreateObject(“roDeviceInfo”).GetLinkStatus()
067:      request = CreateObject(“roUrlTransfer”)
068:      port = CreateObject(“roMessagePort”)
069:*     request.SetMessagePort(port)
070:      request.SetRequest(“GET”)
071:      request.SetUrl(“URL”)
072:      if (request.AsyncGetToString())
073:        while (true)
‘Dot’ Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in pkg:/components/Home/Home.brs(69)
069:     request.SetMessagePort(port)
Backtrace:
#1  Function getmenuitemsbygroup() As Dynamic
   file/line: pkg:/components/Home/Home.brs(69)
#0  Function init() As Dynamic
   file/line: pkg:/components/Home/Home.brs(28)
Local Variables:
global           Interface:ifGlobal
m                roAssociativeArray refcnt=3 count:4
mplinkstatus     Boolean val:true
request          Invalid
port             Invalid
msg             
code            
items           
json            
resultlist      
result          
resultitem      
event           
Threads:
ID    Location                                Source Code
 0    pkg:/source/main.brs(16)                screen.show()
 1*   pkg:/components/Home/Home.brs(69)       request.SetMessagePort(port)
  *selected

Brightscript Debugger> 
Thread detached

Thread detached

Please find the code,

Home.xml :

<?xml version="1.0" encoding="utf-8" ?>         

Home.brs :

function init()
 menuList = getMenuItemsByGroup() 
    for each menu in menuList
        print "menu type is : ";menu.type
        carouselList = getCarouselItemsByType(menu.type)
    end for
end function

Function getMenuItemsByGroup() 
    mplinkStatus = CreateObject(“roDeviceInfo”).GetLinkStatus()
    request = CreateObject(“roUrlTransfer”)
    port = CreateObject(“roMessagePort”)
**    request.SetMessagePort(port)**
    request.SetRequest(“GET”)
    request.SetUrl(“URL”)
    if (request.AsyncGetToString())
      while (true)
          msg = wait(0, port)
          if (type(msg) = “roUrlEvent”)
              code = msg.GetResponseCode()
              if (code = 200)              
                  items = CreateObject(“roArray”, 1, true)
                  json = ParseJSON(msg.GetString())
                  resultList = json.results
                  for each result in resultList
                     resultItem = {
                          title: result.title
                          type: result.name
                          noOfElements: result.pageSize
                          layoutType: result.layoutType
                      }
                      print "result item : ";resultItem
                      items.push(resultItem)
                  end for
                  return items
              endif
          else if (event = invalid)
              request.AsyncCancel()
          endif
      end while
    endif
    return invalid
End Function

Function getCarouselItemsByType(carouselType) as object
    mplinkStatus = CreateObject(“roDeviceInfo”).GetLinkStatus()
    print “link status is :”;mplinkStatus 
    if (not mplinkStatus) then  
        ShowMessageDialog(“Alert”,“No network connection”)
        return invalid
    end if
    request = CreateObject(“roUrlTransfer”)
    print “request is :”;request
    port = CreateObject(“roMessagePort”)
    print “port is :”;port
    request.SetMessagePort(port)
    request.SetRequest(“GET”)
    request.SetUrl(“URL”)
    if (request.AsyncGetToString())
      while (true)
          msg = wait(0, port)
          if (type(msg) = “roUrlEvent”)
              code = msg.GetResponseCode()
              if (code = 200) 
                  items = CreateObject(“roArray”, 1, true)
                  print “items is :”;items
                  json = ParseJSON(msg.GetString())
                  resultList = json.results
                  for each result in resultList
                     print "result is: ";result
                     images = CreateObject(“roArray”, 1, true)
                     imageItems = result.images.values
                     
                     for each imageItem in imageItems
                          image ={
                            profile: imageItem.profile
                            type: imageItem.type
                            link: imageItem.link
                          }
                          print "image link is : "image.link;
                          images.push(image)
                      end for
                      print "images count ";images.Count()
                     resultItem = {
                          id: result._id
                          title: result.title
                          description: result.generalInfo.description
                          images: images
                      }
                      items.push(resultItem)
                  end for
                  return items
              endif
          else if (event = invalid)
              request.AsyncCancel()
          endif
      end while
    endif
  return invalid
End Function

The second diagnostic is confusing even to me, i admit -
but what it means is that you cannot create roUrlTransfer or roMessagePort in the render thread.
See https://sdkdocs.roku.com/display/sdkdoc … pt+Support

So whats the solution? to solve this error?

The solution is right above you. You can’t use roUrlTransfer (and, I imagine, any kind of message port) in the render thread; you have to use it in the main loop or a task node.

The error “roMessagePort: Trying to construct a message port on a non-plugin thread” occurs when the message port is created outside the correct thread context. To resolve this, ensure that the port is constructed in the plugin’s main thread. Use synchronization methods if you’re working with background tasks to ensure proper threading.