"Socket" like Module

Socket Module

Disclaimer: Roblox does not natively support sockets. This module provides a socket-like interface built on top of Roblox’s RemoteEvents for client-server communication.

Github Repo | Download Socket.rbxm

The “Socket” module provides a simple, event-based interface for client-server communication
in Roblox, similar to socket.io. It wraps around RemoteEvents and offers the following key features:

  • Event Listening: Register event listeners using On, remove them with Off, or listen only once with Once.
  • Event Emission: Use Emit to send events to all clients (from the server) or to the server (from the client). Use EmitTo for targeted messaging (server only).
  • Remote Procedure Calls: The Call method allows a client to call a server function and wait for a response within a timeout.
  • Broadcasting: BroadcastExcept lets the server send an event to all players except one.
  • Latency measurement: The Ping method helps measure client-to-server latency.

Features and Usage Examples

1. Event Listening

Register event callbacks on both sides using On.
Example (client-side):

socket:On("HelloFromServer", function(message)
    print("Client received:", message)
end)

On the server side:

socket:On("HelloFromClient", function(player, message)
    print("Server received from", player.Name, ":", message)
end)

2. Emitting Events

Use Emit to send events.
Example (client emitting to server):

socket:Emit("HelloFromClient", "Hi Server, this is the client!")

On the server, you can broadcast to all clients:

socket:Emit("HelloFromServer", "Welcome to the game!")

For targeted messaging (server only), use EmitTo:

socket:EmitTo(targetPlayer, "TargetedMessage", "Hello from server")

3. Remote Procedure Calls (Call)

Clients can call server functions and await a response:

local serverTime = socket:Call("GetServerTime")
print("Client received server time:", serverTime)

On the server, handle the call like a normal event:

socket:On("GetServerTime", function(player)
    return os.time()  -- Returning the server time
end)

4. Broadcasting with Exception

The server can broadcast an event to all players except one using BroadcastExcept:

socket:BroadcastExcept(player, "StateUpdate", newState)

5. Measuring Latency

Measure the ping from client to server with the Ping method:

local latency = socket:Ping()
print("Client measured latency (seconds):", latency)

Module Overview (Socket.luau)

The Socket module abstracts Roblox RemoteEvents to create a socket-like interface.
It supports:

  • On/Off/Once APIs: For registering and removing callbacks.
  • Emit/EmitTo APIs: To send events both broadly and in a targeted manner.
  • Call API: To perform remote procedure calls with automatic timeout handling.
  • BroadcastExcept API: To send events to all players except a specified one.
  • Ping API: To measure client-server latency.

For more detailed usage, inspect the code on github.

Happy coding!

10 Likes

BroadcastExcept would be nice if it used an array of players instead of just 1, overall pretty nice though

I agree! I will make it a feature :slight_smile:

BroadcastExcept now supports an array of players as well as a single player!

1 Like

Added room management functions to Socket: JoinRoom, LeaveRoom, and BroadcastToRoom (server only)

Added middleware support for logging incoming and outgoing events in Socket, also Socket.new supports an identifier, an existing RemoteEvent or nil (for backwards compatibility)


Exemple:

socket:UseOutgoing(function(direction, eventName, args)
    print("[Middleware - Outgoing]", eventName, unpack(args))
    return eventName, args  -- Optionally transform or filter
end)
socket:UseIncoming(function(direction, eventName, args)
    print("[Middleware - Incoming]", eventName, unpack(args))
    return eventName, args
end)

Release page

1 Like

Added ScheduledBroadcast feature to Socket module and update tests. Also added error codes.

The ScheduleBroadcast method allows the server to schedule a broadcast to all clients after a specified delay.

Server Example:

socket:ScheduleBroadcast(10, "EventName", "This message will be broadcasted after 10 seconds.")

Client Example:

Clients can listen for the scheduled broadcast:

socket:On("EventName", function(message)
    print("Received scheduled message:", message)
end)

May you add support for unreliable events as well? Thanks

1 Like

I’ll try to add this today, its a big task so I will probably release it under beta or untested release. Thank you for suggesting.

Added Parallel Socket Operations and Unreliable Events on v0.1.0, its a Pre-release so expect bugs!