Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO without http server?

I am currently working with the Socket.IO fro nodejs to share some real time information between a server and some clients. The clients should only communicate with the server, not with each other.

On windows everything is fine and I can share information, but as it put things onto a linux vps, I have got the problem that he started everything on a ipv6 socket which made it difficult for me to connect with the clientside browser library from socket.io

I start the server like this:

io = require('socket.io').listen(3000); 

which worked fine, and I could connect. Now on the linux vps I tried to prevent ipv6 by using a ipv4 adress like this

io = require('socket.io').listen(3000, '0.0.0.0'); 

but that did not work, not on linux and not on Windows. He does not bind the socket to the ip I want it unless I use the http server like this:

http = require('http').createServer().listen(3000, '0.0.0.0'); io = require('socket.io').listen(http); 

Now my question is, do I have to use the http server to solve the problem of binding the socket to the right local ip and port or is there any other solution? And does socketio start a http server in the background in case of my first line of code, as it is a web socket, or does socketio work without the http server?

like image 786
Arkensor Avatar asked Oct 17 '15 15:10

Arkensor


People also ask

Can you use Socket.IO without server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake.

Does Socket.IO need HTTP?

Websocket is created when you make upgrade from http to websocket, so it kind of does need http. socket.io isn't a pure Websocket server/implementation, it depends on HTTP for its initial connection setup.

Can I use Socket.IO without node js?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes. You will however have Flash dependency.

Is Socket.IO using WebSocket?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.


2 Answers

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.

The issue is that the second parameter in your io.listen(3000, '0.0.0.0') is ignored by Socket.io. If you need to control which network interface to listen on, you should use the code in your last snippet as it is essentially what Socket.io does behind the scenes.

var http = require('http').createServer().listen(3000, '0.0.0.0'); var io = require('socket.io').listen(http); 

WebSocket connections are initiated by the client performing a Protocol Upgrade request over the regular HTTP protocol, which is why an HTTP server is required.

Socket.io on the other hand is a bit different than other WebSocket servers. It uses Engine.IO under the hood, which is an implementation with goals of being reliable. It defaults to emulating a WebSocket connection via XHR / JSONP polling (regular HTTP request-response happening periodically), but upgrades the connection to a WebSocket connection if it is possible. The reason for that is because their research has found that various firewalls, proxies and anti-virus software does in fact block WebSocket connections, and their Engine.IO implementation hides this fact to you without having to implement a fallback solution in case the WebSocket connection is blocked.

like image 186
Svenskunganka Avatar answered Oct 14 '22 13:10

Svenskunganka


You can use 'net' module for creating a socket connection without passing the http parameter. Mostly all socket libraries like Socket.io is built on top of 'net' module.

Here is an example of how to use 'net' module on server side:

const net = require('net');  const server = net.createServer((socket) => {   socket.end(`${new Date()}\n`); }); server.listen(56010); 

Here is how to use on client side:

const net = require('net');  const client = new net.Socket(); client.connect({ port: 56010, host: '127.0.0.1' }); client.on('data', (data) => {   console.log(data.toString('utf-8')); }); 
like image 31
cyperpunk Avatar answered Oct 14 '22 12:10

cyperpunk