MessagingService: "connection has been lost"

Recently I noticed that the server selection menu in my game stopped working. A quick look in the dev console showed this:
image

This is not a custom error() function or anything, but oddly enough its in all lowercase and in quotes.

Code:

local ms = game:GetService("MessagingService")

game.ReplicatedStorage.Events.Servers.GetServers.OnServerEvent:Connect(function(plr, nam)
	local data = {target = plr.Name, place = nam}
	
	ms:PublishAsync(nam.." Server Get", data) -- this line errors
end)

How can I fix this and in what conditions does this happen? I havent seen it anywhere before.

2 Likes

Is that the only error? Sometimes it error there because there’s a problem in the SubscribeAsync script

Yes, thats the only error though it has been printed in the output twice, and not any more times when the event gets fired by a client again

Has anyone experienced this or found out anything about it? I’ve been getting warning prints in my console saying “connection has been lost”

2 Likes

Because this is a Lua error, you can wrap the call to PublishAsync in a pcall (protected call) and perform different logic in the case of an error, for example, you could implement some kind of fall-off retry pattern if this were critical to your game.

Here’s a really quick stab at what that might look like:

local MessagingService = game:GetService("MessagingService")

function tryPublish(topic, message)
  -- This is 4 attempts: immediate, delay 5 seconds, 10 seconds, 20 seconds
  local retry = 0, attemptDelays = {5, 10, 20}
  local success, errorMessage
  while retry <= #attemptDelays do
    if retry > 0 then wait(attemptDelays[retry]) end
    success, errorMessage = pcall(function()
      return MessagingService:PublishAsync(topic, message)
    end)
    if success then
      return true
    end
    retry = retry + 1
  end
  -- out of attempts
  return false, errorMessage
end

-- Now you can use tryPublish(topic, message) to attempt to publish 4 times with exponential
-- falloff over 35+ seconds. Also, you can check the result returned and do something
-- with the message

Note that the example for cross-server messaging includes a pcall, but doesn’t really do anything sophisticated with it.

3 Likes

Thank you for the reply!
I guess this makes sense, I should have used a pcall from the beginning.

But why or when does the function error? Does it just randomly happen, or after a long time has passed?

Also, does this apply to :SubscribeAsync() as well?

Asynchronous methods exist to deal with long-running tasks. They may be for long-running computations like path finding happening on another CPU thread or GPU device, or I/O to player devices or network communication. They can fail for many, many reasons - network issues, service outages, timeouts, misconfigurations, etc. It’s not random, but at the same time, failure is not predictable. If you depend on an async task, you need to have a plan for what to do when it fails or it will error and that will propagate until it is caught by a pcall or the message is dumped to the output / log.

Sometimes the unexpected happens.