Communicating Between Server and Clients
Common Use Cases and there Relevant Events and Methods

Server Side Event and Method Examples
Client Side Event and Method Examples
Note
To start, ensure your server.ts and client.ts are identical to below
./src/server/server.ts
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 | import express from 'express'
import path from 'path'
import http from 'http'
import socketIO from 'socket.io'
const port: number = 3000
class App {
private server: http.Server
private port: number
private io: socketIO.Server
constructor(port: number) {
this.port = port
const app = express()
app.use(express.static(path.join(__dirname, '../client')))
this.server = new http.Server(app)
this.io = new socketIO.Server(this.server)
this.io.on('connection', (socket: socketIO.Socket) => {
console.log('a user connected : ' + socket.id)
})
}
public Start() {
this.server.listen(this.port)
console.log(`Server listening on port ${this.port}.`)
}
}
new App(port).Start()
|
./src/client/client.ts
| class Client {
private socket: SocketIOClient.Socket
constructor() {
this.socket = io()
}
}
const client = new Client()
|
Useful Links
Emit Cheatsheet
https://socket.io/docs/emit-cheatsheet/