Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Handle Socket

Dmitrii Goriunov edited this page Feb 18, 2019 · 8 revisions

JavaScript

Page Content:

Note: that send method allows only direct server <-> client communication, don't use send method to communicate between different clients as it will not scale, for communication between different clients use Pub/Sub system.

Listen on events from the server

To listen on events which are send from the server use on method provided by socket:

// Listen on method from client
socket.on('myeventname', (data) => {
  // your code to execute on event
})

Send events to the server

To send message to the server use send method provided by socket:

// you can send any 'data'
socket.send('nameoftheevent', data)

Note: do not to send events which starts with # or reserved events such as disconnect, connection, error.

ClusterWS JavaScript Client reserved events:

// executed when client is connected to the server
socket.on('connect', () => {
  // your code to execute on connect event
})

// executed when error has happened
socket.on('error', (err) => {
  // your code to execute on error event
})

// executed when client is disconnected from the server
socket.on('disconnect', (code, reason) => {
  // your code to execute on disconnect event
})