We’ll create a simple Node.js server that will act as a relay between GitHub and your Unity application. The server will receive the webhook from GitHub and notify the Unity application.
-
Initialize a Node.js Project: Open a terminal and create a new directory for your project, then navigate into it:
mkdir notification-relay-server cd notification-relay-server npm init -y -
Install Dependencies: Install the required dependencies, namely Express and Socket.IO:
npm install express socket.io
-
Create the Server: Create a file named
server.jsin the project directory:const express = require('express'); const http = require('http'); const socketIo = require('socket.io'); const app = express(); const server = http.createServer(app); const io = socketIo(server); // Middleware to parse JSON payloads app.use(express.json()); // Webhook endpoint to receive GitHub push events app.post('/webhook', (req, res) => { console.log('Push event received:', req.body); // Emit the push event to connected clients (Unity) io.emit('push-event', req.body); // Respond to GitHub res.status(200).send('Webhook received'); }); // Handle socket connection io.on('connection', (socket) => { console.log('Unity client connected'); socket.on('disconnect', () => { console.log('Unity client disconnected'); }); }); // Start the server const PORT = process.env.PORT || 3000; server.listen(PORT, () => { console.log(`Notification relay server running on port ${PORT}`); });
-
Run the Server: Start the server by running:
node server.js
The server will be running on
http://localhost:3000. You can expose this to the internet using services like ngrok to get a public URL.
To expose your server to GitHub, you need a public URL. You can use ngrok for this(You can also use cloudflare ZEROTRUST):
-
Install and Run ngrok:
ngrok http 3000
This will give you a public URL like
http://<your-ngrok-id>.ngrok.io. -
Use the ngrok URL as the Payload URL: In your GitHub repository settings, go to Webhooks and set the Payload URL to
http://<your-ngrok-id>.ngrok.io/webhook. Don't forget to set content type to application/json
Now, create a Unity script to listen for the push events emitted by the server.
You'll need to import a Socket.IO client for Unity. A popular choice is the Socket.IO Unity package.
-
Add the Package using package manager: Copy this url: https://github.com/itisnajim/SocketIOUnity.git
Then in Unity open Window -> Package Manager -> and click (+) add package from git URL... and paste it there.
-
Create a Script: Create a new C# script named
GitHubNotifierand attach it to a GameObject in your scene.using UnityEngine; using SocketIOClient; public class GitHubNotifier : MonoBehaviour { private SocketIO client; void Start() { // Initialize the Socket.IO client and connect to the relay server client = new SocketIO("http://localhost:3000"); client.OnConnected += OnConnected; client.On("push-event", OnPushEvent); client.ConnectAsync(); } private void OnConnected(object sender, System.EventArgs e) { Debug.Log("Connected to the notification relay server"); } private void OnPushEvent(SocketIOResponse response) { // Handle the push event Debug.Log("New push event received from GitHub"); // You can display a notification in Unity using UI elements ShowNotification("New Push to Repo!", "A new commit has been pushed to the repository."); } private void ShowNotification(string title, string message) { // Implement your custom notification UI logic here // For example, using UnityEngine.UI.Text to display the message Debug.Log($"{title}: {message}"); } void OnDestroy() { if (client != null) { client.DisconnectAsync(); } } }
-
Configure and Run Unity:
- Ensure the Unity scene has a GameObject with the
GitHubNotifierscript attached. - Run the Unity scene, and it should connect to the notification relay server.
- Ensure the Unity scene has a GameObject with the
-
Push Changes to the Repo:
- Push a new commit to the GitHub repository where the webhook is configured.
-
Receive Notification in Unity:
- Unity should display a notification (via the
ShowNotificationmethod) when the push event is received.
- Unity should display a notification (via the
This setup allows your Unity application to receive real-time notifications of GitHub push events. The Node.js server acts as a relay between GitHub and Unity, using Socket.IO for real-time communication. Unity then handles the event and displays a notification.
If you want