0

I know there are a lot of articles and notes about this, but I can't find any good client and server examples.

Also, I'm not understanding exactly what should the Nginx server do after the first handshake.

There's an upgrade header, I know that, but from a perspective of a sysadmin, what are the changes that needs to be done so the servers create and maintain open a connection of WS?

For the client side, I wrote this:

window.onload = function() {
    var socket = new WebSocket('wss://echo.websocket.org');

  socket.onopen = function(event) {
    console.log('conectat');
    socket.send("Here's some text that the server is urgently awaiting!");
  };
}

I'm not understanding how to write the server side code, and in what. For the example above, I just want a sample code of the server side that sends back whatever I send to the server. What this code will look like?

I appreciate any schema/sample of code, but please don't provide stuff like "Try Socket.IO". I need something raw so I can understand exactly what is happening.

lmyers
  • 2,654
  • 1
  • 24
  • 22
Abude
  • 2,112
  • 7
  • 35
  • 58
  • possible duplicate of [How can I send and receive WebSocket messages on the server side?](http://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side) – freakish Mar 03 '15 at 12:36
  • Nginx does not have to do anything. Just proxy the connection to the server. WebSocket protocol is just a thin wrapper around raw TCP. – freakish Mar 03 '15 at 12:38
  • @freakish ok so i make a proxy like in : http://nginx.com/blog/websocket-nginx/ , and after that what should be on the server side to listen to connection ?if nodeJS is there any code sample of simple interpretation ? thanks! – Abude Mar 03 '15 at 13:11
  • 1
    It seems that Nginx doesn't do much, it just proxies the connection. So NodeJS has to listen on TCP: http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html Then you have to implement the WebSocket protocol according to rules posted in the SO thread above (with handshake, masking, etc.). That's assuming you don't want to use any library which I guess you should. That stuff although not hard it is time consuming and error prone. – freakish Mar 03 '15 at 15:41
  • If you already know the basics of HTTP requests then read [the RFC](https://tools.ietf.org/html/rfc6455) for details on how to implement a WebSocket server, especially since you haven't specified what language you are looking to implement it in. I just had a go at writing a very minimal server in Java recently and it only took a couple of hours of basically just transcribing the RFC's spec into code. If you don't require something which super-lightweight, though, there are _many_, _excellent_ implementations out there: just Google for _websocket server [insert language here]_. – Saran Tunyasuvunakool Mar 09 '15 at 00:36

0 Answers0