Networking
Apps in Hyperfy communicate using app.send
, app.on
, app.emit
, and world.on
.
Apps execute their scripts in every environment, eg on the server and on every client.
The world.isServer
and world.isClient
allows scripts to determine what environment they are running in.
app.send
: Sends an event to other instances of the same app. When called on a server is sent to all clients, and when called on a client is sent to the server.app.on
: Listens for an event sent from an app (viaapp.send
) in its counter-environment (server or clients).app.emit
: Emits an event for any app to listen to, in the same environment it was called from (either a single client or the server).world.on
: Listens for an event emitted from any app (viaapp.emit
) in the same environment.
Example
App 1
if (world.isServer) {
app.on('ping', () => {
console.log('ping heard on server of original app');
app.emit('cross-app-ping', {});
});
world.on('cross-app-pong', () => {
app.send('end', {});
});
}
if (world.isClient) {
app.on('end', () => {
console.log('full loop ended');
});
app.send('ping', {});
}
App 2
if (world.isServer) {
world.on('cross-app-ping', () => {
console.log('ping heard on different app');
app.emit('cross-app-pong', {});
});
}
Flow
- App 1 (client) sends ping via
app.send
. - App 1 (server) receives ping via
app.on
, emitscross-app-ping
to other apps. - App 2 (server) listens for
cross-app-ping
viaworld.on
, emitscross-app-pong
. - App 1 (server) listens for
cross-app-pong
viaworld.on
, sendsend
to itself. - App 1 (client) receives
end
viaapp.on
, completing the loop.