0

I am using sockets.io with nodejs server to send/receive events between client and server.It works well on local pc but the issue comes when i have to connect a remote client to it and i dont need to use frontend template file or html. In that case, how can i define io() object i.e. a socket connection because without it i cannot access my nodeserver. Currently i have created a js file on remote client and it looks like this:

`var socket=io.connect('http://a.b.c.d:3000');
 var name="alice";
 socket.emit('ping',(name)=>{
 socket.on('pong', reply);
 console.log(reply);
  });`
  • To connect a remote client to your socket.io server, you just need a socket.io client library in the language that your remote client is using. Such client libraries exist in many languages including Java, Javascript, Python, C++, C#, Swift, etc... Then, once you have that client library installed and initialized, you will be able to use code just like you show in your question. Beyond this, it isn't really clear what else you're asking. – jfriend00 Mar 27 '18 at 20:22

1 Answers1

0

Web Sockets work over the HTTP protocol, so you need to run a server for it.

jAmi
  • 779
  • 7
  • 18
  • Thanks @jAmi for your answer. Actually i have to send some data from local nodejs server to remote one. I have already one nodejs server running on my local machine. Using the idea from this link http://www.sebastianseilund.com/json-socket-sending-json-over-tcp-in-node.js-using-sockets i was able to send data from remote server to client (a js file) on my local but now i need to pass it on to my local server so it can show that to browser. – Muhammad Farooq Mar 27 '18 at 15:31
  • Hi @MuhammadFarooq , so on your local server, if you have Socket.io instance connected to your front-end and and JsonSocket connected to your remote instance you can pipe() them both to send data directly to front-end from the remote server – jAmi Mar 28 '18 at 07:23