Skip to content

Room API

Josh Feinsilber edited this page Jul 12, 2019 · 2 revisions

Rooms are where you interact with the game once you are in it! A room on the client links up with a room on the server!

Room Events

onCreate

Called when the Room is created on the server

room.onCreate.add(() => console.log('Room created!'))

onJoin

Called when successfully joining a room

room.onJoin.add(() => console.log('Room joined!'))

onMessage

Called when a new message from the server is sent. Takes in two arguments:

key type optional
action string false
data any true

Example

room.onMessage.add((action: string, data?: any) => {
  if (action === "NEW_ALERT") {
    alert(data)
  }
})

onLeave

Called when you leave a room, either by being kicked or getting disconnected

room.onLeave.add(() => console.log('Left the room!'))

onError

Called when there is an error joining or creating the room. Includes an argument containing the error.

room.onError.add((e) => console.log('Error with the room!', e))

You can clear the listeners from any event by running event.clear() -- for example: client.onLeave.clear()

Room Methods

send

Sends a message back up to the room on the server. Takes two arguments:

key type required
action string true
data any false

Example

room.send("NEW_CHAT_MESSAGE", "Hi there!")
Clone this wiki locally