Hey friends!
I looked far and wide on the web to find a very minimal socket.io implementation. No express, no node http module, just socket.io. I couldn't find a suitable example so I wrote one. I plan on using a version of this on my upcoming MMORPG 2D space sim. Link to the repo here: minsocketio
Clone the repo:
git clone git@github.com:daniellukonis/minsocketio.git
Install the dependencies with:
npm i
then start both the server and client with:
npm start
io.server.js
const io = require("socket.io")(13555)
.on("connection", (socket) => {
socket.on("data", data => io.emit("newData", data))
})
This creates the socket.io server and listens on port 13555. All it does right now is listens for connected clients to send a message labeled data, and rebroadcasts it to all connected clients.
io.client.mjs
import { io } from "socket.io-client"
const socket = io("ws://localhost:13555")
socket.on("newData", newData => console.log(newData))
setInterval(data, 1000)
function data() {
const d = Math.random()
socket.emit("data", d)
}
This client simply connected to the socket.io server and sends a random number. It listens for the server to broadcast a message labeled newData, then logs it to the console.
Thanks for reading! Keep your ears open for progress on my game. I expect to stream some updates soon!
Thanks,
Daniel
Top comments (0)