PHOTON_PROJECT_ID and PHOTON_API_KEY in the process environment. The key
needs project:read access to that project. Keep it on the server.
PHOTON_API_ORIGIN is optional and defaults to https://api.photon.codes.
Every example subscribes again on each connection with the last processed
startSeq. A value of 0 starts with new events; a positive value resumes
after that cursor. Do not add one. See Cursors and replay.
Node.js
Installsocket.io-client (tested with 4.8.3):
Checkpointed examples
The examples below keep up to 128 deliveries in a queue and process one at a time. They retain queued work across reconnects and skip replayed sequences before queueing them again, including the event being processed. The in-memorylast_queued / lastQueued value tracks accepted deliveries; only the processed
cursor is saved or sent in a subscription.
Replace handle_event, handleEvent, or HandleEvent with your idempotent
application handler. They save photon-cursor only after that handler succeeds,
and read it before connecting. Do not launch concurrent work from the handler
and return before it finishes.
Use a separate working directory and cursor file for each project and consumer,
with only one process writing that file. The temporary-file rename avoids a
partially written checkpoint on process restart. It is a local demo store, not a
transaction with your application database or a guarantee against disk failure.
For production, persist the cursor with your application state.
Until the first event succeeds, there is no replay checkpoint. Restarting with
0 starts live again; it does not recover events missed before that first checkpoint.
A handler or checkpoint failure stops the process before it processes later
deliveries. A full queue also exits rather than dropping an event or blocking the
client’s heartbeat. Diagnose the failure, then restart the process from the saved
cursor. Let the library handle network reconnection; do not add a parallel retry
loop. These examples do not send synchronous replies; see
Reply to a webhook event.
Python
Use python-socketio 5.18.1 with its asyncio client. The separate worker serializes deliveries; an async event callback alone does not serialize application work.main.py:
main.py
Go
Use the client package zishang520/socket.io/clients/socket/v3, tested at v3.0.6. The server package alone does not connect to Photon. This client exposespath, WebSocket transport selection, and handshake headers.
main.go:
main.go
Rust
Use rust_socketio 0.6.0 with theasync feature, not the Socket.IO server library socketioxide. Unlike the other clients here, this builder takes the transport path in its URL. Leave the namespace at its default /.
In this version, acknowledgement arguments arrive as an array inside
Payload::Text, so the subscribe result is values[0][0]. Delivery arguments
arrive directly as values[0] (event) and values[1] (metadata).
Create a binary crate with cargo new photon-events, then set these dependencies in Cargo.toml:
src/main.rs:
src/main.rs
Java
Use socket.io-client-java 2.1.2 and Java 11 or later. Add this dependency to your Maven project:PhotonEvents.java in your Java source directory:
PhotonEvents.java
PhotonEvents.main with your project’s Maven or IDE launcher from the directory where its cursor should be stored.
.NET
Use SocketIOClient 4.0.5. This example uses its version 4 callback API and was tested with .NET 10.Program.cs:
Program.cs
Client-specific recovery limits
These are examples, not a shared SDK. Initial connection errors and terminal server disconnects do not have identical retry behavior in every library. Read the connection error, correct the credential or project, and restart when needed. A negative subscribe acknowledgement is also an application error, not a reason to keep advancing the cursor. Rust’semit_with_ack expires the callback after its timeout without calling it.
The .NET example uses the library’s acknowledgement callback without adding an
application watchdog. If a subscribe acknowledgement never arrives on an
otherwise live connection, stop and restart that process; production workers
should monitor subscription readiness.
The Go example uses zishang520’s client because it supports Photon’s current
protocol and required options. Do not substitute the legacy
googollee/go-socket.io client: the tested v1.8.0-rc.1 does not support this endpoint’s
Engine.IO v4, WebSocket-only transport, custom path, and header requirements.