> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# RakNet.OnPacketReceive.Connect

> Connects a callback to the OnPacketReceive signal table and returns a RakNetSignalConnection. The callback receives a RakNetPacket while the packet is valid.

<div className="real-function-page">
  ```lua theme={null}
  RakNet.OnPacketReceive.Connect(callback: (packet: RakNetPacket) -> ()): RakNetSignalConnection
  ```

  ## Aliases

  * `raknet.OnPacketReceive.Connect`

  ## Arguments

  <ParamField path={"callback"} type={"(packet: RakNetPacket) -> ()"} required>
    Function called with each observed incoming RakNetPacket.
  </ParamField>

  ## Returns

  <ResponseField name={"connection"} type={"RakNetSignalConnection"}>
    Connection object with Disconnect and Connected.
  </ResponseField>

  ## Types

  ### RakNetPacket

  Live packet object passed to RakNet hooks and packet signal callbacks. It is only valid during the callback that receives it.

  | Name | Type | Required | คำอธิบาย |
  | - | - | - | - |
  | `SetData` | `(data: string \| buffer \| &#123;number&#125;) -> ()` | Yes | Replaces packet bytes and updates the packet id from the first byte, or 0 for empty data. |
  | `Block` | `() -> ()` | Yes | Marks the packet as blocked. |
  | `Ignore` | `() -> ()` | Yes | Alias that resolves to Block. |
  | `AsString` | `string` | Yes | Packet bytes as a Lua string. |
  | `AsArray` | `&#123;number&#125;` | Yes | Packet bytes as a 1-based array of numbers. |
  | `AsBuffer` | `buffer` | Yes | Packet bytes as a buffer. |
  | `Data` | `buffer` | Yes | Packet bytes as a buffer. |
  | `ID` | `number` | Yes | Packet id value. PacketId, PacketID, and Id resolve to the same value. |
  | `Priority` | `number` | Yes | Packet priority value. |
  | `Reliability` | `number` | Yes | Packet reliability value. |
  | `OrderingChannel` | `number` | Yes | Packet ordering channel value. |
  | `SystemIdentifier` | `string` | Yes | Packet system identifier as decimal string. |
  | `Broadcast` | `boolean` | Yes | Packet broadcast flag. |
  | `ForceReceiptNumber` | `number` | Yes | Packet receipt number. |
  | `Size` | `number` | Yes | Number of bytes in the packet data. |

  ### RakNetSignalConnection

  Connection object returned by RakNet.OnPacketSend.Connect and RakNet.OnPacketReceive.Connect.

  | Name | Type | Required | คำอธิบาย |
  | - | - | - | - |
  | `Disconnect` | `() -> ()` | Yes | Removes the connection from the packet signal. |
  | `Connected` | `boolean` | Yes | true while the connection still exists for the current RakNet hook generation. |

  ## ตัวอย่าง

  Use the signal-style API to observe incoming packets and disconnect the returned connection.

  ```lua theme={null}
  if not RakNet.is_enabled then
      return
  end

  local connection = RakNet.OnPacketReceive:Connect(function(packet)
      print(packet.ID, packet.Size)
  end)

  task.wait(1)
  connection:Disconnect()
  ```
</div>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.