• Stars
    star
    271
  • Rank 151,717 (Top 3 %)
  • Language GDScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GDScript and Godot client for real-time Phoenix Framework Channels

Phoenix Channels Client for Godot and GDScript Godot 3.* Godot 4.0

Donate using PayPal Become a patron Buy Me a Coffee at ko-fi.com

GodotPhoenixChannels is a GDScript and Godot Engine implementation for the Channels API of the Phoenix Framework. It enables Godot projects and games to connect to Phoenix Channels to leverage the connected massive real-time capabilities of Elixir and Phoenix backends. Compatible with Godot 3.* and Godot 4.0.

Links

Godot 4

Godot 3

Installation

Introduction

Before diving in, if you want to see some crazy numbers about the scalability of Phoenix, check The Road to 2 Million Websocket Connections in Phoenix and How Discord Scaled Elixir to 5,000,000 Concurrent Users.

What is Elixir?

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

What is Phoenix?

Phoenix is a web and real-time framework built with Elixir. Phoenix leverages the Erlang VM ability to handle millions of connections alongside Elixir's beautiful syntax and productive tooling for building fault-tolerant systems.

What are Phoenix Channels?

Channels are an exciting part of Phoenix that enable soft real-time communication with and between millions of connected clients. Some possible use cases include:

  • Chat rooms and APIs for messaging apps
  • Breaking news, like "a goal was scored" or "an earthquake is coming"
  • Tracking trains, trucks, or race participants on a map
  • Events in multiplayer games
  • Monitoring sensors and controlling lights

Implementation

This library tries to follow the same design patterns of the official Phoenix JavaScript client, but important changes had to be made regarding events, in order to accommodate to GDScript. Godot's WebSocketClient is used as the transport.

Features

Almost every feature from the JS official client are implemented:

  • Main features of Phoenix Socket (connect, heartbeats, reconnect timers, errors)
  • Main features of Phoenix Channel (join, leave, push, receive, rejoin timers, errors)
  • All features of Presence
  • Automatic disconnection and channel leaving on Node freeing

Examples

Example Godot Project

Example Elixir Project

A simple Elixir server is available in Demo/Server in the demo repository.

To run it, have Elixir installed, then:

cd Demo/server
mix deps.get
iex -S mix phx.server

After the server is running, you can run the Godot demo and in the Host field put: ws://localhost:4000/socket.

Example Usage

var socket : PhoenixSocket
var channel : PhoenixChannel
var presence : PhoenixPresence

socket = PhoenixSocket.new("ws://localhost:4000/socket", {
  params = {user_id = 10, token = "some_token"}
})

# Subscribe to Socket events
socket.connect("on_open", self, "_on_Socket_open")
socket.connect("on_close", self, "_on_Socket_close")
socket.connect("on_error", self, "_on_Socket_error")
socket.connect("on_connecting", self, "_on_Socket_connecting")

# If you want to track Presence
presence = PhoenixPresence.new()

# Subscribe to Presence events (sync_diff and sync_state are also implemented)
presence.connect("on_join", self, "_on_Presence_join")
presence.connect("on_leave", self, "_on_Presence_leave")

# Create a Channel
channel = socket.channel("game:abc", {}, presence)

# Subscribe to Channel events
channel.connect("on_event", self, "_on_Channel_event")
channel.connect("on_join_result", self, "_on_Channel_join_result")
channel.connect("on_error", self, "_on_Channel_error")
channel.connect("on_close", self, "_on_Channel_close")

call_deferred("add_child", socket, true)

# Connect!
socket.connect_socket()

Then you implement the listeners:

#
# Socket events
#

func _on_Socket_open(payload):
	channel.join()
	print("_on_Socket_open: ", " ", payload)

func _on_Socket_close(payload):
	print("_on_Socket_close: ", " ", payload)

func _on_Socket_error(payload):
	print("_on_Socket_error: ", " ", payload)

func _on_Socket_connecting(is_connecting):
	print("_on_Socket_connecting: ", " ", is_connecting)

#
# Channel events
#

func _on_Channel_event(event, payload, status):
	print("_on_Channel_event:  ", event, ", ", status, ", ", payload)

func _on_Channel_join_result(status, result):
	print("_on_Channel_join_result:  ", status, result)

func _on_Channel_error(error):
	print("_on_Channel_error: " + str(error))

func _on_Channel_close(closed):
	print("_on_Channel_close: " + str(closed))

#
# Presence events
#

func _on_Presence_join(joins):
	print("_on_Presence_join: " + str(joins))

func _on_Presence_leave(leaves):
	print("_on_Presence_leave: " + str(leaves))

Push messages to the server:

channel.push("event_name", {some: "param"})

Broadcasts and push replies are received in the event PhoenixChannel.on_event:

channel.connect("on_event", self, "_on_Channel_event")

func _on_Channel_event(event, payload, status):
	print("_on_channel_event:  ", event, ", ", status, ", ", payload)

TODO

See the issues, but mostly:

  • Game example
  • Channel push buffer
  • Socket push buffer

Additional facts about Elixir

As it was shown above, Elixir leverages Erlang, which itself is a programming language used to build massively scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang's runtime system has built-in support for concurrency, distribution and fault tolerance.

Erlang is some 30 years old, built by Ericsson. To give you some context: Ericsson has 45% of the mobile satellite infrastructure in the world. If you are using data in your mobile phone you are certainly in some stage of the day using an equipment that uses Erlang (Source).

More Repositories

1

Godello

Trello inspired kanban board made with the Godot Engine and GDScript, with a real-time collaborative backend (Elixir and Phoenix Channels) and a local backend for offline usage (Godot Custom Resources)
GDScript
772
star
2

GodotDynamicInventorySystem

Godot's fully dynamic inventory system, UI with infinite scrolling, item categories, equipment slots, database of items, inspired by Zelda Breath of the Wild
GDScript
458
star
3

pardall_markdown

Reactive publishing framework, filesystem-based with support for Markdown, nested hierarchies, and instant content rebuilding. Written in Elixir.
Elixir
114
star
4

ecto_instashard

Dynamic Instagram-like PostgreSQL sharding with Ecto
Elixir
55
star
5

GodotShaderCollection

A collection of 3D Shaders for Godot 4
GLSL
46
star
6

GodotRuntimeTextureSplatMapPainting

Texture and Splat Map painting in-game for Godot Engine (also World Vector3 to UV mapping)
GDScript
35
star
7

pardall_markdown_phoenix_demo

Sample reactive Blog and Documentation website with Phoenix LiveView that adapts immediately to Markdown files and folders, powered by PardallMarkdown.
Elixir
15
star
8

Corona-Transition-Manager

OUTDATED AND INCOMPLETE. Pause, Resume and Manages all transitions in your Corona SDK game scenes.
Lua
15
star
9

raylib-cimgui

A pure C raylib backend for the immediate mode gui Dear ImGui, on top of cimgui
C
14
star
10

GodotPhoenixChannels-Demo

Demo project for the Godot Addon: Phoenix Channels
GDScript
10
star
11

GodotInGameVertexPaintingDirtEffect

GDScript
8
star
12

grider

Create and export grids on top of reference photos, a tiny tool for those learning how to draw
GDScript
7
star
13

BooleanLogicGame

A small game made in Godot to teach and to help visualize boolean values in programming logic
GDScript
4
star
14

GodotRunCounter

A simple Godot 4 addon that keeps track of how many times you run (or launch) your Godot project while developing it.
GDScript
3
star
15

elixir_nsq_timeout_test

Sample code to test elixir_nsq timeouts
JavaScript
2
star