MessagingWrapper
is a simple module that allows you to interface with MessagingService in a more object-oriented manner, and allows for sending specific messages and listening to responses as well as sending messages to particular servers.
You can get the module here
Reasons NOT to use this module
-
Only supports Global messages and server specific messages, if you require specific topics you will need to add them yourself, it is relatively easy but can cause weird behaviour. You can get around specific topics by setting the message to a message table containing message type and message data and handle it accordingly.
-
Has not been tested rigorously and is my first module
-
Yields until the server has been assigned a JobID (unless in studio)
-
Can cause issues if you send a global message and all the servers respond so please be weary of how many responses you send to global messages.
Examples
Send a message to the server a specific player is in and listen for a response
local MessagingService = require(script.Parent.MessagingService) -- Require modules and services.
local TeleportService = game:GetService("TeleportService")
MessagingService.OnMessageReceived:Connect(function(MessageData, ResponseObject, SentTime) -- Listen for new messages
print("Received message "..MessageData.Message)
ResponseObject:ChangeMessage("This is a response!") -- Change the current ResponseObjects message
ResponseObject:Send() -- Send the response object
end)
local foundInstance, Error, placeID, JobID = TeleportService:GetPlayerPlaceInstanceAsync(1635038482) -- Get current JobID for specific UserID
local newMessage = MessagingService.new(JobID, "Hello this is my message!") -- Create a new message object
newMessage:Send()-- Send the message
newMessage.OnResponse:Connect(function(data, sentTime) -- Listen for responses to the message
print("received response "..data.Message)
newMessage:Destroy() -- Destroy the message object.
end)
Final notes
If you experience an issue, have some ideas for new functions for MessagingWrapper or notice areas in which the code can be optimized / refactored to provide a better experience then please let me know. Thanks for reading and hopefully you find this module useful and easy to use.
Documentation
Constructors
MessagingWrapper.new(Target, Message)
Returns a new message object.
Target is basically the same as Topic, currently the module supports GLOBAL for global messages and a specific JobID for a server you wish to message.
Methods
Message:Send()
Sends the message object to Target with Message data, will prevent the message from being able to be changed and sent again.
Message:ChangeMessage(newMessage)
Sets the Message objects message to the provided newMessage, can only be called before the message has been sent
Message:ChangeTarget(newTarget)
Changes the Message objects target to the provided newTarget, can only be called before the message has been sent
Message:Destroy()
Destroys the Message object.
Events
Message.OnResponse(MessageData, SentTime)
This event is fired whenever the message receives a response, a message can receive multiple responses.
MessagingWrapper.OnMessageReceived(MessageData, ResponseMessage, SentTime)
This event is called whenever the server receives a message, ResponseMessage is a Message object that is ready to be sent back to the sender using the :Send() method, the message is initially an empty string but can be changed using the Message:ChangeMessage(newMessage) method.
Types
MessageData
Contains a MessageID which is a GUID generated with HTTPService,
SenderID which is the server that sent the messages JobID,
Message which is the user defined message being sent.