Skip to content

Feature: Networker

Tron edited this page Jan 13, 2023 · 2 revisions

━ What's the Objective?

Creating plenty of custom events creates unknowingly alot of issues on performance side. Moreover even the exports provided by FiveM is slower and isn't asper our taste. With our networking solution, you can ditch default events and exports & use await/callbacks. Beyond that we had to do this primarily for our vEngine's network implementation.

━ How to Import?

Add the below code once in either of the shared .lua script of the resource you want to use within:

--Declare it globally only once
loadstring(exports.assetify_library:import("networker"))()

━ APIs

━ assetify.network:getType() (Shared)

@Objective: Retrieve's instance's type.
local string: type = self:getType()

━ assetify.network:create() (Shared)

@Objective: Creates a new network.
local network: cNetwork = assetify.network:create(
  string: name,
  bool: isCallback
)

━ assetify.network:destroy() (Shared)

@Objective: Destroys an existing network.
local bool: result = self:destroy()

━ assetify.network:fetch() (Shared)

@Objective: Fetches network from name.
local network: cNetwork = assetify.network:fetch(
  string: name,
  bool: isRemote
)

━ assetify.network:on() (Shared)

@Objective: Subscribes a handler on a valid network.
--Note #1: Non callback networks can have multiple handlers while callback supports only 1 handler
--Note #2: When Async handling is enabled, first parameter is always a thread instance 
local bool: result = self:on(
  function: exec,
  table: {
    bool: isAsync = true, --Enabling this makes the handler to run asynchronously
    bool: isPrioritized = true, --Enabling this prioritizes the handler to execute in the order they were registered
    int: subscriptionLimit = 5 --Enabling this limits the number of subscription
  }
)

━ assetify.network:off() (Shared)

@Objective: Unsubscribes a subscribed handler from the network.
local bool: result = self:off(
  function: exec
)

━ assetify.network:emit() (Shared)

@Objective: Emits to a network.
--Syntax #1:

--#Local
local bool: result = assetify.network:emit(
  string: name,
  bool: isRemote = false,
  ~: ...arguments
)

--#Client -> Server
local bool: result = assetify.network:emit(
  string: name,
  bool: isRemote = true,
  bool: isLatent,
  ~: ...arguments
)

--#Server -> Client
local bool: result = assetify.network:emit(
  string: name,
  bool: isRemote = true,
  bool: isLatent,
  player: isReceiver,
  ~: ...arguments
)


--Syntax #2 (Local Only):
local bool: result = self:emit(...arguments)

━ assetify.network:emitCallback() (Shared)

@Objective: Emits to a network and awaits for its result.
⚠️ This method is only executable inside a valid thread
--Syntax #1:

--Local
local bool: result = assetify.network:emitCallback(
  string: name,
  bool: isRemote = false,
  ~: ...arguments
)

--#Client -> Server
local bool: result = assetify.network:emitCallback(
  string: name,
  bool: isRemote = true,
  bool: isLatent,
  ~: ...arguments
)

--#Server -> Client
local bool: result = assetify.network:emitCallback(
  string: name,
  bool: isRemote = true,
  bool: isLatent,
  player: isReceiver,
  ~: ...arguments
)


--Syntax #2 (Local Only):
local bool: result = self:emitCallback(
  ~: ...arguments
)