jaxkewl
jaxkewl

Reputation: 213

socket.io endpoints, what/where are they coming from?

In my very simple nodejs server application. I serve up socket.io (v2.2.0) with no express or http. Here is my code.

const io = require('socket.io')();
io.on('connection', (client) => { 
    client.emit("welcome", "hello user");
 });
io.listen(3000);

(Through googling) I've found I can in my web browser, I can navigate to both http://localhost:3000/socket.io/ and http://localhost:3000/socket.io/socket.io.js Where are these 'endpoints' defined from? My code doesn't say anything about those 'endpoints', but yet they exist. Are these leftovers from past versions?

When I go to http://localhost:3000/socket.io/ I get a 400 bad request with a JSON return message of

{"code":0,"message":"Transport unknown"}

and when I go to http://localhost:3000/socket.io/socket.io.js I get this

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.io=e():t.io=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function n(t,e){"object"===("undefined"==typeof t?"undefined":i(t))&&(e=t,t=void 0),e=e||{};var r,n=s(t),a=n.source,p=n.id,f=n.path,l=h[p]&&f in h[p].nsps,d=e.forceNew||e["force new connection"]||!1===e.multiplex||l;return d?(u("ignoring socket cache for %s",a),r=c(a,e)):(h[p]||(u("new io instance for %s",a),h[p]=c(a,e)),r=h[p]),n.query&&!e.query?e.query=n.query:e&&"object"===i(e.query)&&(e.query=o(e.query)),r.socket(n.path,e)}function o(t){var e=[];for(var r in t)t.hasOwnProperty(r)&&e.push(encodeURIComponent(r)+"="+encodeURIComponent(t[r]));return e.join("&")}var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof 
and so on

Upvotes: 1

Views: 2106

Answers (1)

jfriend00
jfriend00

Reputation: 707696

socket.io is based on the webSocket transport. And, the webSocket transport starts every connection with an http request (with certain headers set that indicate it's a webSocket connection request). As such when you do io.listen(), it creates a webServer.

Then, to support some other socket.io stuff (such as fetching the client socket.io library and establishing new connections), it also creates routes based on the /socket.io path prefix such as /socket.io and /socket.io/socket.io.js.

Where are these 'endpoints' defined from?

They are part of the internal workings of socket.io.

Are these leftovers from past versions?

No, they are still how socket.io works.

When I go to http://localhost:3000/socket.io/ I get a 400 bad request

That route is used for new socket.io connections and it requires various query parameters and custom headers in order to do its job properly and that's why you're getting a bad request.

For example, here's a screenshot of the request to start a new socket.io connection using the /socket.io route. You can see both query parameters and custom headers that are part of setting up both the webSocket transport and the socket.io connection on top of that transport.

enter image description here

Upvotes: 1

Related Questions