WebTUT needs to interact with the server to gather updated data from the service. There are two ways to achieve this goal:

 

  1. To interact using a REST API with the PHP Server
  2. To interact using a pre-established Socket.IO connection 

JS Client <-> SOCKET.IO

There are situations where the communications must be done in real-time and the extra effort of establishing a new TCP/IP connection to retrieve data isn't efficient. For those situations, there is a second API that is SOCKET.IO based. It is based upon WebSockets and should be used to retrieve data from server on very regular events, such as, Queue status, Chat messaging, Signalization in general.
The server is configured on both sides of the application:
  • JS Client
    • /app/config.php
  • NodeJS Server
    • config.js
At the end the server will be available using HTTPS on a host and port pre-defined. On that socket, there will be 3 servers in one:
  • HTTP Server - Express Server - to handle minor status information from the server
  • SOCKET.IO Server - to handle requests of data based on "channels"
  • PEERJS - to handle the WebRTC signaling from the PeerJS Client that is used on the frontend
Upon connection a set of channels or contexts are registered on socket.io. Each peer on the connection will use these contexts as addresses to request and deliver information to the correct application module. 
All the methods are based on a prefix name: "get*", "set*", "register*", "unregister*"
The current contexts are:

 

Server Side (Socket.io)Client Side (Socket.io)
getVersionversion
registerToQueueChangeEvent 
unregisterToQueueChangeEvent 
getCurrentQueueInfocurrentQueueInfo

 

With each context, the requesting module can add a data structure that is JSON encoded and is delivered to the context handler. Equally, the answer can have also a supporting structure.
On server the construction is something like this:
  socket.on('getVersion', function(msg){
    console.log("Responding to Version! " + msg);
    socket.emit('version', { version: 1 });
  });
  • No labels