Skip to content

Roblox websocket support made possible thru a server and a roblox module.

License

Notifications You must be signed in to change notification settings

RoSocket/rosocket

Repository files navigation


RoSocket is a Roblox module to emulate a websocket library.
Http requests must be enabled for the module to work.
To enable them, head over to the topbar > FILE > Game Settings > Security > Allow HTTP Requests.
If you are self-hosting, then navigate to the "Reader" module and change "SOCKET_SERVER_URL" to the URL of your server.

GitHub License GitHub Downloads


API documentationExamplesSelf-hosting

Installation

Wally:

[dependencies]
Socket = "RoSocket/[email protected]"

Roblox Model: Click here or Download from Releases (we recommend you get the marketplace one which will always be the latest one)

API

If you want faster replies, then navigate to the reader module > SOCKET_SERVER_UPDATES, set it to 0.10 or less, minimum is 0.02 before ratelimits start to appear.

Functions:

function RoSocket.Connect(socket: string): (any?) -> (table)

Keys:

string RoSocket.Version

Socket:

function socket.Disconnect(...: any): (boolean) -> (boolean)
function socket.Send(msg: string?): (boolean) -> (boolean)
RBXScriptSignal socket.OnDisconnect()
RBXScriptSignal socket.OnMessageReceived(msg: string?)
RBXScriptSignal socket.OnErrorReceived(err: string?)
string socket.UUID -- Universal Unique Identifier
string socket.Socket -- Socket link (e.g: wss://hello.com)
string socket.binaryType -- buffer (doesn't modify way of requests)
string socket.readyState -- OPEN/CLOSED
object socket.Messages
object socket.Errors

Simple Example

local RoSocket = require(script.RoSocket)

-- Http service requests should be enabled for this to work, and a correct server should be set in the Reader module.
local Success, Socket = pcall(RoSocket.Connect, "wss://echo.websocket.org")
if Success ~= false then 
	print(`Socket's Universal Unique Identifier: {Socket.UUID}`) -- ...
	print(`Socket's URL is:	 {Socket.Socket}`) -- wss://echo.websocket.org
	print(`Socket's state is: {Socket.readyState}`) -- OPEN
	print(`Socket's binary Type is: {Socket.binaryType}`) -- buffer (read-only)
	print(`Socket's amount of messages: {#Socket.Messages}`)
	print(`Socket's amount of errors: {#Socket.Errors}`)
	Socket.OnDisconnect:Connect(function(...: any?)
		warn(`Socket {Socket.Socket} was disconnected!`)
	end)
	Socket.OnMessageReceived:Connect(function(msg: string?)
		warn(`Message from {Socket.Socket}: {tostring(msg)}`)
	end)
	Socket.OnErrorReceived:Connect(function(err: string?)
		error(`Error from {Socket.Socket}: {tostring(err)}`)
	end)
	local Suc1 = Socket.Send("Hello World!") -- First message
	print(`Socket first message {Suc1 == true and "has been sent successfully!" or "has failed to send!"}`)
	local Suc2 = Socket.Send("Hello World!") -- Repeated message
	print(`Socket repeated message {Suc2 == true and "has been sent successfully!" or "has failed to send!"}`)
	local Suc3 = Socket.Send("Goodbye World!") -- Second message
	print(`Socket second message {Suc3 == true and "has been sent successfully!" or "has failed to send!"}`)
	Socket.Disconnect()
	Socket.Send("Hello World!") -- Throws a warning in the output saying you can't send messages to a disconnected socket
	print(`Socket's state is: {Socket.readyState}`) -- CLOSED
	print(`Socket's amount of messages: {#Socket.Messages}`)
else
	warn("Failed to connect to websocket!")
end

Self-hosting the server

  1. Download the entire RoSocket repository by clicking on Code > Download ZIP
  2. Extract the ZIP file, and cut the "server" folder. Paste the contents of the folder inside a directory of your choice/folder.
  3. Open a shell and run:
npm install express ws
  1. You're good to go! Optional is to change the default port & default host.

(Back to top)