Socket.io client in NodeJS - node.js

I want to build a microservice infrastructure with nodejs services and a master service, the communication between these services should happen via socket.io, i've setup my socket.io server, but their browser client (socket.io-client) is not working in nodejs (i guess it uses some browser only APIs). Is there a way to create a nodejs socket.io (NOT WEBSOCKETS) client?
EDIT
My client side code:
import { io } from "socket.io-client";
const socket = io("127.0.0.1:3000");
socket.on("connect", () => {
console.log(socket.id);
});
socket.on("disconnect", () => {
console.log(socket.id);
});
My server side code:
import { Server } from "socket.io";
const io = new Server();
io.on("connection", (socket) => {
console.log(socket)
});
io.listen(3000);
Both are written in typescript, the package versions are:
socket.io: ^4.4.0
socket.io-client: ^4.4.0
The Problem is, that i don't get any logs in my console, so i think there is something wrong with client, because socket.io does not mention node in there client side compatiblity graph.

The problem is that you have to pass a valid URL here:
const socket = io("127.0.0.1:3000");
I have no idea why socket.io doesn't give you an error, but if you change that to:
const socket = io("http://127.0.0.1:3000");
Then, it will work.
If you set DEBUG=socket.io-client in your environment, it won't show you an error, but it will show you that it's trying to connect to:
undefined//127.0.0.1:3000
which would give you a clue, I guess.
If you set DEBUG=* in your environment, you will get a lot more debug info (so much that it's a bit hard to sort through).
Set Logging and Debugging Socket.io for more info.

Related

socket.io client side in nodejs app

i am using socket.io for communication between server and client.
My client side is not a html. My client side is javascript file. so here is my code of client side
var io = require('socket.io-client')
var socket = io.connect('http://localhost:3000/home');
socket.on('connect', function () {
console.log(' Connected!');
});
On server side i have received the connection event but on client side connect event doesn't fire. I have tested through html way, it works but why its not working through a java script file.
I have no idea what you mean with tested through html way, but try socket.on("connection", ...),
not "connect".
could be related to the fact that you're specifying a namespace different from the default.
try to handle the connection at http://localhost:3000, instead of http://localhost:3000/home
var socket = io.connect('http://localhost:3000');
found a similar situation here

Socket.io-based app running through node proxy server disconnecting all sockets whenever one disconnects

I made a basic chat app using node.js, express and socket.io. It's not too different from the tutorial chat app for socket.io, it simply emits events between connected clients. When I ran it on port 3001 on my server, it worked fine.
Then I made a proxy server app using node-http-proxy which listens on port 80 and redirects traffic based on the requested url to various independent node apps I have running on different ports. Pretty straightforward. But something is breaking. Whenever anyone disconnects, every single socket dis- and re-connects. This is bad for my chat app, which has connection-based events. The client consoles all show:
WebSocket connection to 'ws://[some socket info]' failed: Connection closed before receiving a handshake response
Here's what I think are the important parts of my code.
proxy-server.js
var http = require('http');
var httpProxy = require('http-proxy');
//create proxy template object with websockets enabled
var proxy = httpProxy.createProxyServer({ws: true});
//check the header on request and return the appropriate port to proxy to
function sites (req) {
//webapps get their own dedicated port
if (req == 'mychatwebsite.com') {return 'http://localhost:3001';}
else if (req == 'someothersite.com') {return 'http://localhost:3002';}
//static sites are handled by a vhost server on port 3000
else {return 'http://localhost:3000';}
}
//create node server on port 80 and proxy to ports accordingly
http.createServer(function (req, res) {
proxy.web(req, res, { target: sites(req.headers.host) });
}).listen(80);
chat-app.js
/*
...other modules
*/
var express = require("express");
var app = exports.app = express(); //I probably don't need "exports.app" anymore
var http = require("http").Server(app);
var io = require("socket.io")(http);
io.on("connection", function (socket) {
/*
...fun socket.on and io.emit stuff
*/
socket.on("disconnect", function () {
//say bye
});
});
http.listen(3001, function () {
console.log("listening on port 3001");
});
Now from what I've read on socket.io's site, I might need to use something to carry the socket traffic through my proxy server. I thought that node-http-proxy did that for me with the {ws: true} option as it states in their docs, but apparently it doesn't work like I thought it would. socket.io mentions three different things:
sticky session based on node's built in cluster module
socket.io-redis, which allows separate socket.io instances to talk to each other
socket.io-emitter, which allows socket.io to talk to non-socket.io processes
I have exactly no idea what any of this means or does. I am accidentally coding way above my skill level here, and I have no idea which of these tools will solve my problem (if any) or even what the cause of my problem really is.
Obligatory apology: I'm new to node.js, so please forgive me.
Also obligatory: I know other apps like nginx can solve a lot of my issues, but my goal is to learn and understand how to use this set of tools before I go picking up new ones. And, the less apps I use, the better.
I think your intuition about needing to "carry the socket traffic through" the proxy server is right on. To establish a websocket, the client makes an HTTP request with a special Upgrade header, signalling the server to switch protocols (RFC 6455). In node, http.Server instances emit an upgrade event when this happens and if the event is not handled, the connection is immediately closed.
You need to listen for the upgrade event on your http server and handle it:
var proxy = httpProxy.createProxyServer({ws: true})
var http = http.createServer(/* snip */).listen(80)
// handle upgrade events by proxying websockets
// something like this
http.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head, {target:sites(req.headers.host)})
})
See the node docs on the upgrade event and the node-http-proxy docs for more.

Socket.IO on Heroku does NOT work without SSL

I have a chat server setup as such:
var port = Number(process.env.PORT || 5000);
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app, {'log level':1, 'match origin protocol':true})
, fs = require('fs')
io.set('authorization', function (handshakeData, callback) {
console.log(handshakeData);
callback(null, true);
});
and then I handle some events:
io.sockets.on('connection', function(socket) {
socket.emit('handshaken', {id:socket.id}); // for HTML clients
socket.on('subscribe', function(roomId) {
doSubscribe(socket, roomId);
});
socket.on('unsubscribe', function(roomId) {
doUnsubscribe(socket, roomId);
});
socket.on('chat', function(data) {
doChat(data);
});
});
The client is on a different domain.
When I use the chat server via https, then everything is working fine. All the events are received. However, when I use http, I can see that the client can receive the 'handshaken' event, but nothing else is sent or received.
I wonder if this has anything to do with the socket.io authorization not working properly with non ssl connection.
However, in local environment, I can still use non ssl http://localhost:5000 as the chat server url without any issue. Is it also possible that this is an issue with Heroku?
UPDATE 1: After some investigation, if I use http url for the chat server, the server can emit to the client. The client can connect to the server, but cannot emit anything to the server (the server does not receive any emit).
Update 2: Some further investigations revealed that the chat server, under http, does received an emit, but only 1 emit. Any emit after that is not received.
It turned out that Sophos antivirus for Mac is the culprit here. After I disabled all web protection, my chat app works fine.
The interesting point here is that Sophos only targets Chrome browser, as Firefox and Safari work without any problem.

Using Meteor and socket.io together

I am newbie about Meteor.
I am developing a realtime multiplayer game. I want to implement everything about the game but game engine states with Meteor. For example chat messages, available game rooms, invitations, online members etc. I want to make those functionalities withMeteor.
But I want to implement game states manually without Meteor with socket.io. Because, the game is real time and every 45 msec(in my architecture), game states will be streaming to the clients and I think Meteor is not for this and not flexiable. So I developed multiplayer concept and synchronising clients and server with socket.io. There is no problem about it.
I want to use Meteor and socket.io both and together. I tried to implement it. I installed socket.io with npm inside .meteor/local/build/programs/server/app under my meteor app. After that I include require statement on server side Meteor startup;
Meteor.startup(function () {
var require = Npm.require;
var sio = require('socket.io')
var socketIO = sio.listen(this.http)
socketIO.configure(function () {
socketIO.set('log level', 0);
socketIO.set('authorization', function (handshakeData, callback) {
callback(null, true); // error first callback style
});
socketIO.set("transports", ["xhr-polling"]);
socketIO.set("polling duration", 30);
});
socketIO.sockets.on('connection', function (client) {
console.log(client.id + ' is connected')
client.on('disconnect', function () {
console.log(client.id + ' is diconnected')
});
})})
And I put the connection statement on client side Meteor startup;
Meteor.startup(function () {
socket = io.connect();
socket.on('connect', function () {
console.log('connecting');
});
})
On client side, io variable is not defined error is occurred. This is seen to me that,Meteor does not import client side socket.io.js on client side. So I tried to put socket.io.js manually under clients folder to load it on client side. This is not good way I know, I should not do this. But, even I do and client loads it, there is another client side error about transport variable of io for the statement;
io.transports.push('xhr-polling');
It says that Uncaught TypeError: Cannot call method 'push' of undefined. Somehow, client side socket.io.js can not be loaded properly.
I could not find an example for usage of Meteor and socket.io together. Is there a simple way to use them both together?
Thank you!

socket.io example sometimes not connecting client side when using a reverse proxy

Using node-http-proxy, I've set up a reverse proxy for routing requests:
var httpProxy = require('http-proxy');
var server = httpProxy.createServer({
hostnameOnly: true,
router: {
'www.example.com': 'localhost:3002'
}
}).listen(80);
Now, when I run the first example on http://socket.io/#how-to-use, the socket is sometimes not connecting with the client. I created two files to test this: server.js and index.html. To start the node application, i run server.js.
server.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(3002);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
console.log("Socket connected");
});
index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
setInterval(function() {
console.log(socket.socket.connected);
}, 1000)
</script>
When the client does not connect, after the socket connects with the server, I repeatedly get the following output with intervals of +/- 10 seconds:
debug - setting request GET /socket.io/1/xhr-polling/Cqcw5xUjQ-B-Hw3FGF7Y?t=1385128607702
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client Cqcw5xUjQ-B-Hw3FGF7Y
Now, when I refresh the browser a few times, the socket always connects with the server (that is, it always logs "Socket connected"), but sometimes it does not connect client side: console.log(socket.socket.connected) sometimes repeatedly prints "false" after refreshing index.html, and after another page refresh, it may repeatedly print "true" or "false" again if the socket did not or did connect with the client.
The example does work client-side when I do not use the reverse proxy, so when I run server.js on port 80 on www.example.com. It would be great if someone could point me out what could be the cause of this problem. I am using node.js v0.8.23, socket.io version 0.9.14 and node-http-proxy version 0.10.1.
UPDATE
Probably, I am actually using node v0.10.21. I thought I was using v0.8.23 by switching the node version using nvm, but for some reason it keeps switching back to v0.10. It is a known issue that http-proxy does not support web sockets for node versions later than 0.8, so that may be the cause. I am using robertklep's solution until I find something better.
I think the issue might be that socket.io is initially trying to use WebSockets as a transport medium and when that doesn't work (I don't know if node-http-proxy can proxy WS connections), it falls back to a transport that does work (xhr-polling) but the client and/or server gets confused in the process.
Try disabling the websocket and flashsocket transports to see if that makes it more reliable:
io.set('transports', [ 'xhr-polling', 'jsonp-polling', 'htmlfile' ]);
(more info)
It really was the node version I was using. Node-http-proxy does not work for node version >0.8 (see Node http proxy with proxytable and websockets). I thought I was using v0.8.23 but I was actually using v0.10.21. By using n, I could get node working for v0.8.23 for sure, and now it seems to work. I highly recommend n for resetting the node version.

Resources