Network Interface API

Networking is provided by the Network Module block, accessed through the peripheral system. Place a Network Module adjacent to a computer and wrap it to obtain the net handle.

Setup

local nm  = peripheral.wrapRelative("front", "networkmodule")
local net = nm.getNet()

Typical usage

local nm  = peripheral.wrapRelative("front", "networkmodule")
local net = nm.getNet()

net.setHostname("miner-01")

net.send("miner-02", "job", "start")
if net.hasMessage("status") then
  local msg = net.receive("status")
  print(msg.getSender(), msg.getContent())
end

net.openChannel("ops", "")
net.sendChannel("ops", "", "hello channel")

Direct messaging

  • setHostname(name)

  • getHostname()

  • send(targetHostname, protocol, message)

  • receive(protocol)

  • hasMessage(protocol)

  • broadcast(protocol, message)

Channels

  • openChannel(channelName, password)

  • closeChannel(channelName)

  • sendChannel(channelName, password, message)

  • receiveChannel(channelName)

  • hasChannelMessage(channelName)

Local channels (same sector)

  • openLocalChannel(channelName, password)

  • closeLocalChannel(channelName)

  • sendLocal(channelName, password, message)

  • receiveLocal(channelName)

  • hasLocalMessage(channelName)

Entity scope (same ship/station)

  • sendEntity(protocol, message)

  • receiveEntity(protocol)

  • hasEntityMessage(protocol)

Delivers protocol messages to other computers on the same entity.

Computer scope (this computer only)

  • sendComputer(protocol, message)

  • receiveComputer(protocol)

  • hasComputerMessage(protocol)

Useful for coordination between foreground commands and background scripts on one computer.

Networked Data Stores

  • getDataStore(name) Returns a RemoteDataStore handle for the named Networked Data Store, or nil if the name is not registered. The handle allows reading and writing data without the owning entity being loaded.

local nm  = peripheral.wrapRelative("front", "networkmodule")
local net = nm.getNet()

local store = net.getDataStore("faction-prices")
if store then
    print(store.getValue("iron_ore"))
    store.set("iron_ore", "200")
end

Discovery helpers

  • getHostnames()

  • getCurrentSector()

  • isHostnameAvailable(name)

  • ping(targetHostname)

Message object

Objects returned by receive* methods expose:

  • getSender()

  • getContent()

  • getRoute()

  • getTransport()

  • getTimestamp()