1

I am new in socket.io. I wanted to develop an small application using it. I know the basic of socket.io. I have followed this and this documentation.

I am also able to make this simple connection. Code :

Client:

<script src="/socket.io/socket.io.js"></script>
  <script>
        console.log(location.protocol+'//'+location.host);
                var socket = io.connect(location.protocol+'//'+location.host), socketwithApi = io.connect(location.protocol+'//'+location.host + '/apikey/');
  </script>

    <script type="text/javascript">
        var i=0;
              $(document).on('click','#socket-emit-btn', function(){
                let message = location.href;
                socket.emit("test",message);

              });
              socket.on("testlistened",function(data){
                  i ++;
                  console.log(i);
                  console.log(data);
                  if(data){
                    $("#console-div").html(data);
                  }else{
                    $("#console-div").html("{empty}");
                  }
                });

              socketwithApi.on('apikey',function(data){
                   console.log(data);
                });
    </script>

Server:

   //consider everything needed is defined
    var apikey = io
      .of('/apikey/')
      .on('connection', function (socket) {
        socket.emit('apikey', { news: 'item' });
    });

    io.on('connection', function (socket) {
        let clientID = socket.id
        let clientIP = socket.handshake.address
        if (client) client.socket = socket
        console.log('User-Client Connected!: IP: ' + clientIP)

        socket.on("test",function(data){
            console.log("listened");
            let ParsedData = url.parse(data);
            let responseData = {
                UrlScheme:ParsedData,
                socketID: clientID,
                api_key: ParsedData.pathname
            }
            console.log(responseData);
            console.log(`${TRANSACTIONGETRECEIPT}_RESPONSE`)
            socket
                .binary(true)
                .emit('testlistened', JSON.stringify(responseData))
        });

        socket.on('disconnect', function (data) {
            console.log(clientID + ' has disconnected')
        })
    })

Everything is working fine, i am able to emit and listen both from the server side and client side. But I did not find the documentation which would tell if it is possible to send http get or post request from the browser or postman to socket.io . Is it only possible to send request from a page where socket client is present or can we also send request like

  http://localhost:8099/api-hastgraph-key/socket.io/?EIO=3&transport=websocket&sid=w0kDs4oGI7SVwB3YAAAC
  or http://localhost:8099/socket.io/api-hastgraph-key?EIO=3&transport=websocket&sid=w0kDs4oGI7SVwB3YAAAC

from browser and connect to socket.

Thank you. Any kind of help are highly appreciated.

Tekraj Shrestha
  • 1,228
  • 3
  • 20
  • 48
  • 1
    you just want to find out if it is possible, or you have sth in mind that you wanna do with this knowledge? – hackape Apr 17 '19 at 01:10
  • 1
    It's possible, of course to connect with bare http, without the help of socket-io client js. Check [this link](https://developers.google.com/web/updates/2015/05/replay-a-network-request-in-curl). You can copy paste curl into [postman](https://stackoverflow.com/questions/43067726/how-to-convert-curl-to-postman). If you understand how socket-io protocal works, you can mannually setup a connection by sending the right request. – hackape Apr 17 '19 at 01:17
  • @hackape Hello hackape, thank you for the reply. I want to create an api for a browser extension that will be using this socket connection. Also the link you gave soesn't say much about socket.io . What I want to do is, perform the action done by socket client library with some url. eg: socket.emit("test",message); and listion this test event in server. – Tekraj Shrestha Apr 17 '19 at 06:33
  • 1
    The link I gave just show how to copy-paste a request as-is from browser to postman. For your goal, you can still use socket-io client js to establish a connection, then you just expose the client `socket` object to a global namespace, e.g. simply attach it to `window.socket`, I mean, why not? Now your browser extension just do `window.socket.emit('test', msg)`, job done. – hackape Apr 17 '19 at 06:52
  • 1
    @hackape Thank you so much for the tips. I will try implementing it, hope it will work. Thanks. – Tekraj Shrestha Apr 17 '19 at 06:56
  • @hackape hello, is it possible, to bind all the socket connection to a routing (both listening and emitting from server side ) and call that route and perform the specified action? Or say I want clients to provide a unique identifier to each socket connection. What is the best way to do that ? – Tekraj Shrestha Apr 22 '19 at 14:36
  • 1
    So you want a single endpoint (in other word, single URL) to handle all connections, both `socket.io` websocket connection and normal http request? Short answer yes, you can do that. But what real life problem are you solving this time? – hackape Apr 22 '19 at 17:09
  • @hackape I am asked to provide an url for an API that should connect through socket. Suppose url would be like this: xxx.com/api_key/socket.io?some=query&EIO=xxxxxxxx . Now I need to provide socket connection on this URL. Or Is there any way I can trace the connection for one particular user everytime it gets connected to the socket ? Suppose if a user abc is connected to socket today and used api for 3 times, now when user connects to the same socket it count should start from 4.... How can we connect to nodejs socket without connecting to socket.io from url only. Is it possible ? – Tekraj Shrestha Apr 23 '19 at 06:33
  • 1
    This question is unanswered, so I think it's fine you just edit it to reflect your new goal, include some example maybe. I might look into it later. This conversation in comments is not efficient, currently I don't fully understand what you want. – hackape Apr 23 '19 at 06:38

0 Answers0