**NOTE**: NOT UP TO DATE
This was written back in 2019/2020, and not updated later, even though the websocket
API changed. Sadly the websocket API that exists now is now partially undocumented.
import { Channel, Socket } from "phoenix";
const gamesocket = new Socket("ws://localhost:4000/socket", {
params: { token: "token" }
});
gamesocket.connect();
const gamechannel = gamesocket.channel("game:tite", {});
gamechannel
.join(5000)
.receive("ok", resp => {****
console.log("Joined successfully", resp);
});
gamechannel.push("game:get_status", {}).receive("ok", (reply) => {
// Do something
});
gamechannel.on("game:player_connected", (payload) => {
// do something
});
Join channel with name game:tite
, where tite
is one of the guild ids:
tite
otit
digit
tik
cluster
Client can push these messages to the server
gamechannel.push("game:get_status", {}).receive("ok", (reply) => {
// Do something
});
Command: game:begin_cast
{
"spell_id": 123
}
Note: The cooldown timestmap (time when spell is ready again) is returned in UTC time.
Attack reply:
{
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
},
"type": "attack",
"damage": {
"value": 39,
"min_damage": 10,
"max_damage": 50,
"is_crit": false
},
"cooldown": "2020-02-25T19:13:42.943446Z"
}
Global buff reply:
{
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
},
"type": "global_buff",
"damage": null,
"cooldown": "2020-02-25T19:13:42.943446Z"
}
Personal buff reply:
{
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
},
"type": "buff",
"damage": null,
"cooldown": "2020-02-25T19:13:42.943446Z"
}
Command: game:get_status
{}
Reply:
{
"id": "tite",
"guild": "Tampereen TietoTeekkarikilta",
"guild_id": "1",
"zone_id": "1",
"players": ["Cadiac", "Shancial"],
"max_players": 1000,
"cooldowns": {
"1": "2020-02-29T21:40:44.921080Z",
"2": "2020-02-29T21:40:48.780592Z",
"3": "2020-02-29T21:40:52.749782Z",
"4": "2020-02-29T21:40:56.711034Z",
"5": "1970-01-01T00:00:00Z",
"6": "2020-02-29T21:41:19.144154Z",
"7": "1970-01-01T00:00:00Z",
},
"buffs": [
{
"id": "026dbbd4-fe29-4e78-8a9e-21f274293923",
"cooldown_multiplier": 0.7,
"crit_multiplier": 1,
"damage_multiplier": 1,
"duration_ms": 30000,
"expires_at": "2020-02-29T19:01:49.439560Z",
"icon_url": "https://sivu.dev/images/icons/spell_nature_bloodlust.jpg",
"name": "Bloodlust"
}
],
"spells": [
{
"cast_time": 1500,
"cooldown": 0,
"icon_url": "http://localhost:4000/images/icons/spell_fire_flamebolt.jpg",
"id": 1,
"name": "Fireball",
"required_level": 1,
"type": "attack",
"description": "Hurls a fiery ball that causes 123 to 151 Fire damage."
},
{
"cast_time": 0,
"cooldown": 8000,
"icon_url": "http://localhost:4000/images/icons/spell_fire_fireball.jpg",
"id": 2,
"name": "Fireblast",
"required_level": 6,
"type": "attack",
"description": "Blasts the enemy for 12 to 40 Fire damage."
},¨
{
"cast_time": 6000,
"cooldown": 12000,
"icon_url": "http://localhost:4000/images/icons/spell_fire_fireball02.jpg",
"id": 4,
"name": "Pyroblast",
"required_level": 14,
"type": "attack",
"description": "Hurls an immense fiery boulder that causes 211 to 231 Fire damage"
},
{
"cast_time": 0,
"cooldown": 300000,
"icon_url": "http://localhost:4000/images/icons/spell_holy_magicalsentry.jpg",
"id": 5,
"name": "Arcane Intellect",
"required_level": 10,
"type": "global_buff",
"description": "Infuses the target's raid with brilliance, increasing their damage and critical strike chance for 5 minutes."
},
{
"cast_time": 0,
"cooldown": 180000,
"icon_url": "http://localhost:4000/images/icons/spell_holy_magicalsentry.jpg",
"id": 7,
"name": "Arcane Power",
"required_level": 30,
"type": "buff",
"description": "When activated, your spells deal 30% more damage. This effect lasts 30 sec."
}
],
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
},
"zone": {
"cleared": false,
"cleared_at": "2020-02-29T19:01:49.439560Z",
"unlocked": true,
"unlocked_at": "2020-02-29T19:01:49.439560Z",
"max_level": 20,
"min_level": 10,
"name": "Tietotalo",
"required_kills": 100,
"current_kills": 1,
"background_image_url": "https://sivu.dev/images/zones/tietotalo.jpg"
}
}
Clients can listen for these messages as they are broadcasted to all clients on the channel
gamechannel.on("game:player_connected", (payload) => {
// do something
});
Message: game:player_connected
{
"id": 12345678,
"username": "Cadiac"
}
Message: game:player_disconnected
{
"id": 12345678,
"username": "Cadiac"
}
Message: game:npc_updated
{
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
},
"zone": {
"cleared": false,
"cleared_at": "2020-02-29T19:01:49.439560Z",
"unlocked": true,
"unlocked_at": "2020-02-29T19:01:49.439560Z",
"max_level": 20,
"min_level": 10,
"name": "Tietotalo",
"required_kills": 100,
"current_kills": 1,
"background_image_url": "https://sivu.dev/images/zones/tietotalo.jpg"
}
}
Message: game:npc_updated
{
"npc": {
"name": "Mottled Boar",
"max_hp": 200,
"hp": 200,
"level": 1,
"image_url": "http://example.com"
}
}
Message: game:buff_gained
{
"id": "026dbbd4-fe29-4e78-8a9e-21f274293923",
"cooldown_multiplier": 0.7,
"crit_multiplier": 1,
"damage_multiplier": 1,
"duration_ms": 30000,
"expires_at": "2020-02-29T19:01:49.439560Z",
"icon_url": "https://sivu.dev/images/icons/spell_nature_bloodlust.jpg",
"name": "Bloodlust"
}
Message: game:buff_faded
{
"id": "026dbbd4-fe29-4e78-8a9e-21f274293923",
"cooldown_multiplier": 0.7,
"crit_multiplier": 1,
"damage_multiplier": 1,
"duration_ms": 30000,
"expires_at": "2020-02-29T19:01:49.439560Z",
"icon_url": "https://sivu.dev/images/icons/spell_nature_bloodlust.jpg",
"name": "Bloodlust"
}
types: "chat"
, "join"
, "quit"
, "server"
Message: game:server_message
{
"text": "Mottled Boar dies, you gain 576 experience.",
"type": "server"
}
Payload:
{
"reason": "Error text here"
}
Authenticated user can join its own personal channel with name user:#{user_id}
.
Clients can listen for these messages as they are broadcasted to them on the channel
gamechannel.on("user:exp_updated", (payload) => {
// do something
});
Message: user:exp_updated
{
"experience": 1250,
"experience_required": 5000,
"experience_change": 75,
"total_experience": 12500,
"level":5,
"is_levelup": false,
"message": "Mottled Boar dies, you gain 75 experience."
}
Message: user:buff_gained
{
"id": "026dbbd4-fe29-4e78-8a9e-21f274293923",
"cooldown_multiplier": 0.7,
"crit_multiplier": 1,
"damage_multiplier": 1,
"duration_ms": 30000,
"expires_at": "2020-02-29T19:01:49.439560Z",
"icon_url": "https://sivu.dev/images/icons/spell_nature_bloodlust.jpg",
"name": "Bloodlust"
}
Message: user:buff_faded
{
"id": "026dbbd4-fe29-4e78-8a9e-21f274293923",
"cooldown_multiplier": 0.7,
"crit_multiplier": 1,
"damage_multiplier": 1,
"duration_ms": 30000,
"expires_at": "2020-02-29T19:01:49.439560Z",
"icon_url": "https://sivu.dev/images/icons/spell_nature_bloodlust.jpg",
"name": "Bloodlust"
}
Message: user:spell_unlocked
{
"cast_time": 1500,
"cooldown": 0,
"icon_url": "http://localhost:4000/images/icons/spell_fire_flamebolt.jpg",
"id": 1,
"name": "Fireball",
"required_level": 1,
"type": "attack",
"description": "Hurls a fiery ball that causes 123 to 151 Fire damage."
}
Authenticated user can join global chat channel chat:global
.
Client can push these messages to the server
chatchannel.push("chat:send_global_message", { text: "LFG RFC" }).receive("ok", (reply) => {
// Do something
});
Command: chat:send_global_message
{
"text": "LFG RFC"
}
No reply.
Clients can listen for these messages as they are broadcasted to them on the channel
chatchannel.on("chat:chat_message", (payload) => {
// do something
});
Message: chat:chat_message
{
"from": {
"id": 123456789,
"username": "Cadiac",
"guild_id": 1,
"level": 15
},
"text": "LFG RFC",
"timestamp": "2020-02-25T19:13:42.943446Z",
"type": "chat"
}