A Godot plugin for creating real-time interactions with the Pusher Channels Protocol over a WebSocket connection.
Plugin version | Godot version | Pusher Channels Protocol version |
---|---|---|
1.0 | 3.5.1 | 7.0 |
2.0 | 4.3 | 7.0 |
Move the ./addons folder into your project folder.
See full guide: Installing a plugin
Enable the plugin, see full guide: Enabling a plugin
Create an account and then create a Channels app. See full guide
Pusher.connect_app( APP_KEY, { "cluster": APP_CLUSTER } )
func connected_handler(data):
print("Hello!")
func error_handler(data):
print("Oh No!")
Pusher.connection_established.connect(connected_handler);
Pusher.connection_error.connect(error_callback);
Authentication happens when you call the signin method
:
Pusher.signin()
See full documentation
Before your app can receive any events from a channel, you need to subscribe to the channel. Do this with the subscribe
method:
var channel = Pusher.subscribe("channel-name")
Every published event has an “event name”. For your app to do something when it receives an event called "my-event", your web app must first “bind” a function to this event name. Do this using the channel’s bind
method:
func event_handler(event_data):
print(event_data)
channel.bind("my-event", event_handler)
You can only trigger a client event once a subscription has been successfully registered:
var channel = Pusher.subscribe("channel-name")
channel.bind(PusherEvent.SUBSCRIPTION_SUCCEEDED, handle_subscription);
func handle_subscription():
channel.trigger("client-someEventName", { "message": "hello!" })
See full documentation
It is possible to set and update the client configuration through the configure
method:
Pusher.configure(KEY, OPTIONS)
Params of the configure
method:
Param | Type | description |
---|---|---|
key | string | The application key |
options | dictionary | See below |
Based on Pusher channels options parameter:
Options | Type | description |
---|---|---|
cluster | string | The cluster used by your application |
userAuthentication | dictionary | See below |
channelAuthorization | dictionary | See below |
Based on Pusher userAuthentication object:
Option | Type | description |
---|---|---|
params | dictionary | Additional parameters |
headers | dictionary | Additional HTTP headers |
endpoint | string | URL endpoint of server for authentication. |
Based on Pusher channelAuthorization object:
Option | Type | description |
---|---|---|
params | dictionary | Additional parameters |
headers | dictionary | Additional HTTP headers |
endpoint | string | URL endpoint |
By default nothing will be log to the debug console. If you want to debug your application and see what’s going on within Channels then you can set the debug
value to true:
Pusher.debug = true
A connection to Pusher Channels can be established by invoking the connect_app
method, the configuration can be established by passing the same params from configure method:
$Pusher.connect_app(APP_KEY, { "cluster": APP_CLUSTER })
You may disconnect by invoking the disconnect_app
method:
Pusher.disconnect_app()
You can monitor the state of the connection so that you can notify users about expected behaviour. See guide: connection states API
You can access the current state as:
Pusher.connection_state
To listen for connection state changes use the connection_state_changed signal:
$Pusher.connection_state_changed.connect(callback)
All state names are available as constants trough the PusherState
class:
// PusherState.CONNECTED == "connected"
See full list: Available connection states
The default method for subscribing to a channel involves invoking the subscribe
method:
Pusher.subscribe("my-channel");
To unsubscribe from a channel, invoke the unsubscribe
method:
$Pusher.unsubscribe("my-channel");
If a channel has been subscribed to already it is possible to access channels by name, through the get_channel
method:
var channel = Pusher.get_channel("channel-name")
Use the bind
method of the pusher node to listen for an event on all the channels that you are currently subscribed to:
$Pusher.bind(EVENT, CALLBACK);
Params of the bind
method:
Param | Type | description |
---|---|---|
event | string | The name of the event to bind to. |
callback | Callable | Called whenever the event is triggered. |
Events can be bound to directly on a channel. This means you will only receive an event if it is sent on that specific channel:
channel.bind(eventName, callback)
Use unbind
to remove a binding of an event:
Pusher.unbind(EVENT, CALLBACK)
Params of the unbind
method:
Param | Type | description |
---|---|---|
event | string | The name of the event from which your want to remove the binding. |
callback | Callable | Callable used when binding to the event. |