mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-09 15:08:12 -05:00
"connect" and "connection" have the same meaning, but "connection" is the preferred version.
29 lines
631 B
TypeScript
29 lines
631 B
TypeScript
import { Server } from "socket.io";
|
|
|
|
const io = new Server(8080, {
|
|
cors: {
|
|
origin: "http://localhost:4200",
|
|
methods: ["GET", "POST"]
|
|
}
|
|
});
|
|
|
|
interface Todo {
|
|
completed: boolean;
|
|
editing: boolean;
|
|
title: string;
|
|
}
|
|
|
|
let todos: Array<Todo> = [];
|
|
|
|
io.on("connection", (socket) => {
|
|
socket.emit("todos", todos);
|
|
|
|
// note: we could also create a CRUD (create/read/update/delete) service for the todo list
|
|
socket.on("update-store", (updatedTodos) => {
|
|
// store it locally
|
|
todos = updatedTodos;
|
|
// broadcast to everyone but the sender
|
|
socket.broadcast.emit("todos", todos);
|
|
});
|
|
});
|