Skip to content

Conversation

@rayhanadev
Copy link
Member

@rayhanadev rayhanadev commented Jun 5, 2025

The dashboard stores Discord messages in a Neon database, causing us a ton of headache when we hit egress limits. Also its lowk weird to store Discord messages in a database anyways.

This API will allow authenticated clients to push messages to connected dashboard instances, primarily to be used with https://github.com/purduehackers/wack-hacker.

Usage

  1. Connect a bot to wss://api.purduehackers.com/discord/bot.
  2. Send a message { "token": "<DISCORD_API_ENDPOINT_KEY>" } using the API key stored in .env on the server.
  3. Receive { "auth": "complete" } from the server.
  4. Send JSON payloads to the server.
type DiscordMessage = {
  image: string;
  timestamp: string;
  username: string;
  content: string;
  attachments?: string[];
};
  1. Connect a dashboard (or many) to wss://api.purduehackers.com/discord/dashboard.
  2. Receive the above JSON messages whenever emitted.

Minimal Code Samples

Bot Code:

import WebSocket from "ws";

const ENDPOINT = "wss://api.purduehackers.com/discord/bot";
const ws = new WebSocket(ENDPOINT);

ws.on("open", () => {
  console.log("[bot] socket open, sending token ...");
  // API KEY REQUIRED ON BOT SIDE TO CONNECT
  ws.send(JSON.stringify({ token: process.env.DISCORD_API_KEY }));
});

function sendDiscordMessage() {
  const msg = {
    image: "https://cdn.discordapp.com/avatars/636701123620634653/ef93aabe488676f4e772c3592469bcb1?size=1024",
    timestamp: new Date().toISOString(),
    username: "rayhanadev",
    content: "Hello, world!",
    attachments: [], // optional
  };
  ws.send(JSON.stringify(msg));
  console.log("[bot] message pushed");
}

ws.on("close", () => console.log("[bot] connection closed"));
ws.on("error", (err) => console.error("[bot] error:", err));

setInterval(sendDiscordMessage, 1000);

Dashboard Code:

const ENDPOINT = "wss://api.purduehackers.com/discord/dashboard";
const ws = new WebSocket(ENDPOINT);

ws.onopen = () => console.log("[dashboard] connected");
ws.onclose = () => console.log("[dashboard] disconnected");
ws.onerror = (e) => console.error("[dashboard] error", e);

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  console.log(msg)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants