-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsocketio-server.js
37 lines (31 loc) · 1.13 KB
/
socketio-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const socketIo = require("socket.io");
const createHttpServer = require("../server-helpers");
const { port } = require("./constants");
const startSocketIoServer = () => {
const { server: socketIoServer } = createHttpServer();
const io = socketIo(socketIoServer, {
cors: {
origin: "http://localhost:1234",
methods: ["GET", "POST"]
}
});
io.on("connection", function (socket) {
socket.on("one-last-message", () => {
socket.emit("last-messages", "This is one new message");
});
socket.on("three-last-messages", () => {
socket.emit("last-messages", "This is one new message");
socket.emit("last-messages", "This is a second new message");
socket.emit("last-messages", "This is a third new message");
});
socket.on("three-messages", () => {
socket.emit("new-message", "This is one new message");
socket.emit("new-message", "This is a second new message");
socket.emit("new-message", "This is a third new message");
});
});
socketIoServer.listen(port, () =>
console.log(`[Socket.io] Started on port :${port}`)
);
};
module.exports = startSocketIoServer;