openOMSI GitHub Discord
Writing plugins

Docs / Plugins / Writing plugins

Talking to other plugins and programs

3 min readEdit this pagedocs/PLUGINS.md

On this page
  1. Other plugins
  2. Other players
  3. Programs on this computer
  4. WebSocket feeds
  5. The telemetry file

Other plugins#

omsi.plugin.send(to, topic, data) sends to one plugin by name, omsi.plugin.broadcast(topic, data) to all others. They receive it in their next frame as the message(from, topic, data) event. Data can be numbers, texts, booleans and tables of them. omsi.plugin.list() names the loaded plugins.

Other players#

In a LAN game, omsi.lan.send(player, text) reaches the same plugin on another player's game (0: every other player). It arrives as lan_message(from, text). Texts are short (120 characters, no |), about ten a second at most, carried in the session's own messages. A game without the plugin ignores them.

omsi.lan.session() reads the host's name, map, date, clock, weather and season as announced when this player joined; no plugin can change them. Both need the lan permission.

Programs on this computer#

omsi.send(port, text) sends text as one UDP datagram to 127.0.0.1:port: to an overlay, a dashboard or a company's tracker on the same computer. It never reaches the network. It does not wait and nothing comes back. A message sent while no program listens is lost, so keep what must not be lost in omsi.storage too.

omsi.send returns true when the message was handed to the system. It returns false and the reason when:

  • the port is below 1024, or one of the game's multiplayer ports (27015 to 27024);
  • the message is longer than 8 KB;
  • the plugin already sent 100 messages in the last second;
  • the system refused it.
Lua
-- the speed and the next stop, twice a second, for a program on port 47800
omsi.every(0.5, function()
  local d = omsi.duty.get()
  omsi.send(47800, omsi.json.encode({
    speed = omsi.speed(),
    next_stop = d and d.next_stop and d.next_stop.name,
  }))
end)

To see what arrives, run nc -lu 47800 in a terminal.

WebSocket feeds#

omsi.ws.open(url) opens a read-only feed to a wss:// address in one of three slots; omsi.ws.state(slot) says whether it is connecting, open, reconnecting, failed or closed; omsi.ws.poll(slot) takes the text messages that arrived (oldest first, at most 32 kept, each up to 64 KB); omsi.ws.close(slot) frees the slot.

The connection runs on its own thread, so the game never waits, and a broken feed opens again after 5 seconds. Nothing is ever sent to the server.

The player allows hosts in websocket-hosts.txt in the plugins folder: one host per line (feeds.example.org, or a whole wss://feeds.example.org/live), # starts a comment, at most the first three lines count. With no file, or an empty one, nothing is opened.

A ws:// address, a port of its own, a user name or password, a literal IP address (even when it is on the list), a redirect and a host not on the list are refused. These functions need the network_remote permission.

The telemetry file#

A program that only wants to follow the player's bus (a fleet map, an in-vehicle terminal, a stream overlay) can read ~/.openomsi/telemetry.json instead of being a plugin. On Windows it is %USERPROFILE%\.openomsi\telemetry.json.

  1. Create the file once, empty.
  2. The game fills it from then on, about twice a second. It replaces the whole file each time, so a reader never sees half of it.
  3. Delete the file to stop the writing.

The file stays on this computer. A file whose updated stops changing belongs to a game that ended.

Field Contents
version 2
pid, updated, paused the game's process, Unix seconds, paused or not
player null without a bus, else x, y, z, tile_x, tile_y, local_x, local_y, heading, speed_kmh, delay_s, passengers
duty null without a duty, else the fields below
ai_buses the timetable buses on the road

duty holds line, tour, trip, trip_index, trip_count, terminus, next_stop_index, next_stop_id, next_stop_name, next_stop_dist, prev_stop_id, at_stop and stops. Each stop has object_id, name, stops, arr, dep, x, y.

Each entry of ai_buses has id, line, tour, trip, terminus, depart, next_stop_id, at_stop, trip_done, delay_s, x, y, number. Stop ids are the map's object ids, as in omsi.info().

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open