1

I have a node.js server and I attached socket.io listener to it. The code is like this.

const server = new Hapi.Server();
    server.connection({
        "port": config.port
    });

let io = socketio(server.listener);
io.on("connection", function(socket) {
        console.log("A user connected");

        socket.on("disconnect", function(){
            console.log("A user disconnected");
        });

        // receive message from client
        socket.on("client-server", function(msg) {
            console.log(msg);
        });
    });

// somewhere to emit message
io.emit("server-client", "server to client message");

Normally I use the standard way to connect to the websocket server. An example is like this:

<!DOCTYPE html>
<html>
  <head><title>Hello world</title></head>
  <script src="http://localhost:3000/socket.io/socket.io.js"></script>
  <script>
    var socket = io();
    socket.on('server-client', function(data) {document.write(data)});
    socket.emit('client-server', 'test message');
  </script>
  <body>Hello world</body>
</html>

It works without issue. Now, my colleague wants to connect to the websocket server from his FME server. Based on his research, the only way he can use to connect to a websocket server is using a url like this:

ws://localhost:3000/websocket

My question is: is there a way to connect to socket.io server listener using this type of string?

If not, is there a way to create a websocket server with ws://host:port url and also attach it to my node.js server?

Or, is there a way to connect to socket.io listener in FME server?

zhangjinzhou
  • 2,461
  • 5
  • 21
  • 46
  • 1
    Your colleague needs to get a socket.io client library that he can use from his server. Socket.io is its own protocol on top of webSocket so you need a socket.io-compatible client library in order to be able to connect to a socket.io server. – jfriend00 Apr 01 '17 at 02:20
  • I see. The issue is that he can't do any coding work on his FME server. Therefore there is no way for him to setup a socket.io client. A good new is that we can deploy some python script to FME server. Can a python socket.io client connect to my socket.io server created in node.js? – zhangjinzhou Apr 01 '17 at 17:15
  • Yes, there is socket.io support in python. – jfriend00 Apr 01 '17 at 17:22
  • My socket.io server is created by Javascript. How to use python to connect to javascript server? Do you know any good example? @jfriend00 – zhangjinzhou Apr 01 '17 at 17:24
  • Just search for python socket.io and you will find what you need. I'm not a python person myself. – jfriend00 Apr 01 '17 at 17:44
  • Yes I did find the package. Will be doing research on it. Thank you very much! – zhangjinzhou Apr 01 '17 at 17:49

1 Answers1

0

To tell Socket.IO to use WebSocket only, add this on the server:

io.set('transports', ['websocket']);

And on the client add this:

var socket = io({transports: ['websocket']});

Now you can only connect to the WebSocket server using ws protocol.

Yi Kai
  • 630
  • 6
  • 9
  • He still need to create a client right? He can't do any coding however. – zhangjinzhou Apr 01 '17 at 17:17
  • I guess in this case, instead of Socket.io, https://github.com/websockets/ws will be better. A generic WebSocket server library. –  Jun 21 '18 at 16:16