Skip to content

Websockets#11

Open
sofalere wants to merge 2 commits into
mainfrom
websockets
Open

Websockets#11
sofalere wants to merge 2 commits into
mainfrom
websockets

Conversation

@sofalere

Copy link
Copy Markdown
Owner

WebSocket:

  • bidirectional communication protocol that can send the data from the client to the server or from the server to the client
  • using socket.io the main functions are emit and on, either one can be used in the frontend or the backend
  • each connection to our app will have its own WebSocket, thats why in the server side we send the "new request" event to many sockets

server side:

  • require (node:http) that is used for creating an HTTP server, this is necessary because WebSockets use the HTTP connection to establish the WebSocket connection
  • the Socket.io library will create the WebSocket server
  • the io object waits for connections
  • emits a new event called "new request" when request hits our route to all current open sockets
  const request = await saveRequest(req);
  io.sockets.emit('new request', request);
  res.sendStatus(200);
});

client side:

  • <script src="/socket.io/socket.io.js"></script>source the file before the js app file
  • const socket = io(); initiates the connection to the WebSocket server
  • listens for a socket event called "new request"
    const listItem = createRequestItem(request);
    requestList.appendChild(listItem);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant