Skip to main content
Participant
January 10, 2025
Answered

Question about plugin socket communication

  • January 10, 2025
  • 1 reply
  • 767 views

I have a question about LrSocket's receive process

startRecvHttpServer = function()
    LrTasks.startAsyncTask(function()
        LrFunctionContext.callWithContext("sign_in", function(context)
            local running = true
            local serverSocket = LrSocket.bind({
                functionContext = context,
                plugin = _PLUGIN,
                port = 9999,
                mode = "receive",
                onConnecting = function(socket, port)
                end,
                onConnected = function(socket, port)
                end,
                onError = function(socket, errorMsg)
                end,
                onMessage = function(socket, data)
                    logger.Info("Receive Callback:  " .. data)
                    running = false
                end,
                onClosed = function(socket)
                    running = false
                end,
            })
            while running do
                LrTasks.sleep(1 / 2)
            end
            serverSocket:close()
        end)
    end)
end

 

The following code makes localhost:9999 wait to receive a message.

In this state, type http://locahost:9999/xxx in the address bar of the browser and onMessage will be called. This is the expected behavior, but strangely enough, the plugin sends “ok” data to the browser on its own.
The following is the actual data sent as filtered by Wireshark.

---

0000 02 00 00 00 45 00 00 2c c2 37 40 00 80 06 00 00 ....E..,.7@.....
0010 7f 00 00 01 7f 00 00 01 27 0f c5 26 41 40 24 1c ........'..&A@$.
0020 db 9c 2d 1d 50 18 27 f9 b3 0b 00 00 6f 6b 0d 0a ..-.P.'.....ok..

---

This behavior is not expected. The browser raises ERR_INVALID_HTTP_RESPONSE when it receives “ok

Why does the plugin send “ok”?
I would like to know if this is a bug or a specification.

Correct answer kimaldis

John is correct, it's always been the case that you need 2 processes, one for send and the other to receive and each on their own port. It makes things complicated at the other end and I've no idea why they chose to do it this way.
I've found using a URLhandler, which uses the x-callback standard to send and receive, makes things a lot simpler.  Jeffrey Friedl gives a good explanation here: https://regex.info/blog/2012-01-17/1926 . There will be a performance hit if you're sending a lot of data, though.

 

1 reply

johnrellis
Legend
January 10, 2025

"the plugin sends “ok” data to the browser on its own."

 

I think LrSocket has always worked that way, e.g. see:

https://community.adobe.com/t5/lightroom-classic-discussions/lrsocket-win10-sending-messages-via-sockets/m-p/10572260

 

An LrSocket can only be used to recieve or send data, but not both. I think that's why others haven't complained about LrSockets in receive mode sending the "ok" response. (Who knows why LrSocket was "designed" this way.)

 

If you want another program to send a request and then get a response from your plugin, you have two options:

 

- Use two LrSockets, one in receive mode and one in send mode. This is inconvenient, to say the least.  

 

- Use LrHttp URLHandler. See here for an explanation of how it works:

https://regex.info/blog/2012-01-17/1926

Participant
January 10, 2025

johnrellis

 

Thank you for the helpful information. I will consider it.