Skip to content

Commit

Permalink
Create MessagingClient.luau
Browse files Browse the repository at this point in the history
  • Loading branch information
nonNamedUser authored Oct 24, 2024
1 parent c04681d commit 5c7a132
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/MessagingClient.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export type MessagingClient={
Publish: (self: MessagingClient, topic:string, message: any) -> (),
Subscribe: (self: MessagingClient, topic:string, callback: (message: any) -> ()) -> ()
}

--[[
-- _ _ ____ _ ____ _____
-- | | | / ___| / \ / ___| ____|
-- | | | \___ \ / _ \| | _| _|
-- | |_| |___) / ___ \ |_| | |___
-- \___/|____/_/ \_\____|_____|
--
-------------------------------------------------------------
Publish:
local MessagingClient=require(path.MessagingClient)
local TOPIC="Example"
MessagingClient.Publish(TOPIC, "Example")
-------------------------------------------------------------
Subscribe:
local MessagingClient=require(path.MessagingClient)
local TOPIC="Example"
MessagingClient.Subscribe(TOPIC, function()
end)
]]

local MessagingService = game:GetService("MessagingService")

local MessagingClient = {}

function MessagingClient:Publish(topic, message)
local success, err = pcall(function()
MessagingService:PublishAsync(topic, message)
end)

if not success then
warn("Failed to publish message to topic ["..topic.."]: "..err)
end
end


function MessagingClient:Subscribe(topic, callback)
local success, err = pcall(function()
MessagingService:SubscribeAsync(topic, function(message)
callback(message.Data)
end)
end)

if not success then
warn("Failed to subscribe to topic ["..topic .."]: "..err)
end
end

return MessagingClient

0 comments on commit 5c7a132

Please sign in to comment.