A FiveM resource that serves as a wrapper around the Socket.io library, allowing you to connect to arbitrary Socket.io servers, subscribe to events, and publish data from your FiveM server.
- Clone this repository into your FiveM resources folder
- Add
ensure fivem-socket.ioto your server.cfg - Build the resource with:
pnpm install pnpm build
-- Connect to a socket server
local connected = exports['fivem-socket.io']:socketConnect('http://localhost:3000', '/')
if connected then
print('Connected to socket server')
-- Subscribe to an event
-- The second parameter is the callback event name that will be triggered when the event is received
exports['fivem-socket.io']:socketSubscribe('http://localhost:3000', '/', 'server-event', 'client:socket:data')
-- Listen for the callback event
AddEventHandler('client:socket:data', function(data)
print('Received data from socket server:')
print(json.encode(data))
end)
-- Publish an event to the socket server
exports['fivem-socket.io']:socketPublish('http://localhost:3000', '/', 'client-event', {message = 'Hello from FiveM!'})
-- Later, when you're done with the socket
exports['fivem-socket.io']:socketUnsubscribe('http://localhost:3000', '/', 'server-event')
exports['fivem-socket.io']:socketDisconnect('http://localhost:3000', '/')
end-- Create connection options with headers and params
local options = {
-- Connection options
reconnectionAttempts = 5,
reconnectionDelay = 2000,
timeout = 10000,
autoConnect = true,
-- Custom headers for authentication
headers = {
Authorization = "Bearer token123",
["x-api-key"] = "your-api-key"
},
-- URL parameters to send with the connection
params = {
userId = "123456",
clientVersion = "1.0.0"
}
}
-- Connect to a socket server with options
local connected = exports['fivem-socket.io']:socketConnect('http://localhost:3000', '/', options)
if connected then
-- Subscribe to an event with the same options
exports['fivem-socket.io']:socketSubscribe(
'http://localhost:3000',
'/',
'server-event',
'client:socket:data',
options
)
-- Publish an event with options
exports['fivem-socket.io']:socketPublish(
'http://localhost:3000',
'/',
'client-event',
{message = 'Hello from FiveM!'},
options
)
end-- Example of a comprehensive options object
local options = {
-- Connection options
reconnectionAttempts = 5,
reconnectionDelay = 2000,
timeout = 10000,
autoConnect = true,
-- Custom headers for authentication
headers = {
Authorization = "Bearer token123",
["x-api-key"] = "your-api-key",
["Content-Type"] = "application/json",
["User-Agent"] = "FiveM-Socket.io-Client"
},
-- URL parameters to send with the connection
params = {
userId = "123456",
clientVersion = "1.0.0",
serverRegion = "us-west",
debug = true
}
}
-- The headers and params will be included in the socket connection
local result = exports['fivem-socket.io']:socketConnect('http://localhost:3000', '/', options)
-- They will also be included in subscribe operations
exports['fivem-socket.io']:socketSubscribe('http://localhost:3000', '/', 'user-updates', 'client:user:update', options)
-- And in publish operations
exports['fivem-socket.io']:socketPublish('http://localhost:3000', '/', 'client-action', { action = "login" }, options)See the examples folder for more detailed usage examples.
These exports can be called from both server and client-side Lua scripts:
socketConnect(url, namespace, options)- Connect to a socket serversocketSubscribe(url, namespace, event, callbackEvent)- Subscribe to an eventsocketUnsubscribe(url, namespace, event)- Unsubscribe from an eventsocketPublish(url, namespace, event, data)- Publish an eventsocketDisconnect(url, namespace)- Disconnect from a socketsocketDisconnectAll()- Disconnect from all sockets
These events can be triggered from both server and client-side Lua scripts:
socket:connect- Connect to a socket serversocket:subscribe- Subscribe to an eventsocket:unsubscribe- Unsubscribe from an eventsocket:publish- Publish an eventsocket:disconnect- Disconnect from a socketsocket:disconnectAll- Disconnect from all sockets
This library is structured with the following components:
- SocketManager: Core class handling Socket.io connections and interactions
- FiveMSocketAdapter: Adapter that exposes functionality through FiveM exports and events
- Validation Module: Separate validation utilities for input sanitisation and validation
validators.ts: Contains URL, namespace, event, and options validationsanitisers.ts: Contains data sanitisation functionsindex.ts: Exports all validation utilities