Use express as proxy for websocket - javascript

I have a data provider which gives me stock prices via TCP connection. The data provider only allows a static IP to connect to their service.
But since I need to format the data before sending it to my front-end I want to use my express back-end as a proxy.
What that means is:
I need to connect my back-end to my data provider via websocket(socket.io) in order to get the data (back-end acts as client)
I need my back-end to broadcast this received data to my front-end(back-end acts as server)
My question is: Is that possible at all? Is there an easier way to achieve this? Is there a documentation on how to use an express app as websocket server and client at once?
EDIT:
I got this working now. But my current solution kills my AWS EC2 instance because of huge CPU usage. This is how I've implemented it:
const net = require('net');
const app = require('express')();
const httpServer = require('http').createServer(app);
const client = new net.Socket();
const options = {
cors: {
origin: 'http://someorigin.org',
},
};
const io = require('socket.io')(httpServer, options);
client.connect(1337, 'some.ip', () => {
console.info('Connected to some.ip');
});
client.on('data', async (data) => {
// parse data
const parsedData = {
identifier: data.identifier,
someData: data.someData,
};
// broadcast data
io.emit('randomEmitString', parsedData);
});
client.on('close', () => {
console.info('Connection closed');
});
httpServer.listen(8081);
Does anyone have an idea why this causes a huge CPU load? I've tried to profile my code with clinicjs but I couldn't find a apparent problem.
EDIT2: To be more specific: My data provider provides my with stock quotes. So every time a quote changes, I get new data. I then parse this data and emit it via io.emit. Could this be some kind of bottleneck?
This is the profile I get after I run clinicjs:

I don't know how many resources you have on your AWS, but 1,000 clients shouldn't be a problem.
I have personally encountered 2 bottlenecks:
Clients connected with Ajax, not WS (It used to be a common problem with old socket.io)
The socket.io libraries were served by Node, not Nginx / Apache. Node is poor at keeping-alive management.
Check also:
How often do you get data from some.ip? Good idea is aggregate and filter it.
Do you need to notify all customers of everything? Is it enough just to inform interested? (Live zone)
Maybe it is worth moving the serving to serviceWorker.js or Push Events?
As part of the experiment, log yourself events. Receiving data, connecting and disconnecting the client. Observe the server logs.
As part of the debugging process, log events. Receiving data, connecting and disconnecting the client. Observe the server logs.
Or maybe this code is not responsible for the problems, but the data download for the first view. Do you have data in the buffer, or do you read for GET index.html?

To understand what was going on with your situation, I created an elementary TCP server that published JSON messages every 1ms to each client that connects to it. Here is the code for the server:
var net = require('net');
var server = net.createServer(function(socket) {
socket.pipe(socket);
});
server.maxConnections = 10
server.on('close', () => console.log('server closed'))
server.on('error', (err) => console.error(err))
server.on('listening', () => console.log('server is listening'))
server.on('connection', (socket) => {
console.log('- client connected')
socket.setEncoding('utf8')
var intervalId = setInterval(() => socket.readyState === "open" &&
socket.write(JSON.stringify({
id: intervalId,
timestamp: Date.now(),
}) + '\n'), 1)
socket.on('error' , (err) => console.error(err))
socket.on('close' , () => {
clearInterval(intervalId)
console.log('- client closed the connection')
})
})
server.listen(1337, '0.0.0.0');
As you see, we set up a setInterval function that will emit a simple JSON message to each connected client every 1 ms.
For the client, I used something very similar to what you have. At first, I tried pushing every message received by the server to the browser to the WebSocket connection. In my case, it also pushed the CPU to 100%. I don't know exactly why.
Nonetheless, even though your data is being updated every 1 ms, it is doubtful that you need to refresh your webpage at that rate. Most websites work at 60 fps. That would mean updating the data every 16ms. So, a straightforward solution would be to batch the data and send it to the browser every 16 ms. Just this modification greatly increases performance. You can go even further by extending the batch time or filtering some of the sent data.
Here is the code for the client, taking advantage of batch messages. Bear in mind that this is a very naive implementation made to show the idea. A better adjustment would be to work the streams with libraries like RxJS.
// tcp-client.js
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const net = require('net')
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const client = new net.Socket()
app.get('/', (req, res) => {
res.setHeader('content-type', 'text/html')
res.send(`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TCP - Client</title>
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('msg', (msg) => document.body.textContent = msg);
</script>
</body>
</html>
`);
});
io.on('connection', (socket) => {
console.log('- user connected');
socket.on('disconnect', () => {
console.log('- user disconnected');
});
});
var buffer = []
setInterval(() => {
io.emit("msg", JSON.stringify(buffer))
buffer = []
}, 16)
client.connect(1337, '127.0.0.1', function() {
console.log('- connected to server');
});
client.on('data', function(data) {
buffer.push(data.toString("utf8"))
});
client.on('close', function() {
console.log('- connection to server closed');
});
server.listen(3000, () => {
console.log('listening on 0.0.0.0:3000');
});

Related

Websockets not working on live nodejs app and nextjs app

this code is in my nodejs backend (https://backend.example.com) server.js file:
const WebSocket = require('ws');
const server = new WebSocket.Server({
port: 7500
},
() => {
console.log('Server started on port 7500');
}
);
This code is in my nextjs frontend chat (http://frontend.example.com/chat) page file:
React.useEffect(() => {
ws.current = new WebSocket("ws://localhost:7500");
ws.current.onopen = () => {
console.log("Connection opened");
setConnectionOpen(true);
};
ws.current.onmessage = (event) => {
const data = JSON.parse(event.data);
setMessages((_messages) => [..._messages, data]);
};
return () => {
console.log("Cleaning up...");
ws.current.close();
};
}, []);
it works fine in localhost but on deployed live server, the websocket is not communicating, what is wrong with my code?
EDIT: Have updated the useEffect() to:
React.useEffect(() => {
ws.current = new WebSocket("wss://backend.example.com:7500");
ws.current.onopen = () => {
console.log("Connection opened");
setConnectionOpen(true);
};
ws.current.onmessage = (event) => {
const data = JSON.parse(event.data);
setMessages((_messages) => [..._messages, data]);
};
return () => {
console.log("Cleaning up...");
ws.current.close();
};
}, []);
but still it does not work, If I visit the https://backend.example.com I get Upgrade Required
If this is the code you deploy on live server, then I think the following points have to be addressed.
On the client you point to localhost, you should have the server name, instead.
And more important, in local you're publishing the app in http, while in live server it is in https?
In this case the WebSocket url should change the protocol from ws to wss.
UPDATE
Another point of attention is your server.
I don't see code that is handling the connection, according to the documentation example.
import WebSocket from 'ws';
const ws = new WebSocket('ws://www.host.com/path');
// This handle the co
ws.on('open', function open() {
ws.send('something');
});
ws.on('message', function message(data) {
console.log('received: %s', data);
});
If this is not the library you're using, please update your question whit all the references to the library you're using or the documentation you're referring to to write your code.
About the port, there is nothing specific about the port, the only issue is that you can have the port blocked, on the firewall of your client or on the network gateway of your server.
But that depends on your environment, you should check if the port is usable.
A simple test is to try a small server with an html page published on the 7500 port on your server. If you can see the page the port is ok.
And more you should not use the same port of your server, pick another one, because the http server is reserving that port, and your WebSocket will fail attempting to bind.
But you should see an error on the server if that happened.
If you want to use your application server port, instead of starting a different server then follow this example.

Encoding of socket data not working with Node

// server.ts
import { Server } from 'net';
const port = 8081;
const hostname = 'localhost';
const server = new Server();
server.on('connection', (socket) => {
socket.setEncoding('utf8');
socket.on('data', (data) => {
console.log(data.toString());
});
});
server.listen({ port, host: hostname }, () => {
console.log('Server running at port', port);
});
When I try to console log the data of the buffer I am getting weird chars. I tried to assign it to a variable and split the data of the first request, but the same is happening. I tried to set 'utf-8' as well and all the other types and no one is giving me a the request properly.
This is the string I am getting when decoded to 'utf8':
��Lϻط�4�)]�3O�X\��M�ʏ����S�mN 9BRg����Vl�^A:�ʃ�"I���r�*��p� ���+�/�,�0̨̩����/5����
**
#
3+)** http/1.1
���]xO����
�[��ˈ�
I�B,}�J��
-+
ih2zz�
My purpose is to get the websocket key of the request. Then I will respond with the hash in base 64 to establish the connection, but that's another thing, only to put in context the situation.
PD: I am using Node 18.7.0, but I tried with 16 and the same happens.
PD2: Okay, I realized the problem. At the client I was setting a wss://localhost:8081. Switching this to ws://localhost:8081 solved the problem, however I am still interested in the solution for a secure connection.
Thank you.

NodeJS/Express/WebSockets Server Restarts After 2 Minutes

Background
I have a NodeJS/Express application that uses WebSockets for certain endpoints that need to hold a connection for backend functions that take several minutes to complete.
An example of one of these functions is the creation and processing of an Excel document that is sent back to the client for download.
Problem
The creation of this Excel document is triggered by a button click on the frontend. After the button is clicked, a WebSocket message is sent to the server that identifies an Excel document needs to be created. A function is called that begins creating the document. As part of this document's creation, a number of API and function calls are made, which causes the whole process to take several minutes to complete. After 2 minutes of waiting for the document to be created and sent back, the server restarts, preventing the document from being created and sent to the client.
Important Notes
When I test my code locally, everything works as intended. Furthermore, I have a heartbeat (ping/pong) setup so the WebSocket connection isn't closed from inactivity. After the WebSocket connection is made, the connection stays active indefinitely; however, clicking the button that causes the Excel document to be created is what causes the server to restart after 2 minutes.
Code Snippets
Code for setting up the application, routes, WebSocket connections, etc.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server:server });
// More Application Setup Here...
wss.on('connection', function connection(ws) {
ws.on('message', async function incoming(message) {
if (message.toString() == '__ping__') {
ws.send('__pong__');
return;
}
let msg = JSON.parse(message);
switch(msg.call) {
case "create_excel":
var workbook = await create_excel();
ws.send(workbook); // Workbook is converted to buffer in real code
break;
default:
break;
}
});
});
// Routes Here...
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Listening on port ${port}...`));
Client code for connecting to socket and sending request for file:
const socket = new WebSocket("wss://myurl.com/path");
socket.onopen = function() {
setInterval(ping, 30000);
};
socket.onmessage = function(event) {
if (event.data == "__pong__") {
pong();
return;
}
if (typeof event.data == 'object') {
# Call function to save file
return;
}
# Handle other messages here...
};
function ping() {
socket.send('__ping__');
tm = setTimeout(5000);
}
function pong() {
clearTimeout(tm);
}
button.onclick = function() {
socket.send(JSON.stringify({ call: "create_excel" }));
}
Attempted Fixes
Prior to using WebSockets, I just used HTTP requests for this same function; however, that would fail after 2 minutes as well in the form of a 504 Gateway Error. I transitioned to WebSockets hoping to fix this issue. I've tried the solutions suggested here with no luck.
Could this be a server configuration problem? Are there other options for trying to increase the timeout?

How to use socket.io to display realtime data in the client (web browser) from the node.js server?

I want to display the real-time speech to text data in the browser. By real-time what I mean is, "while I am speaking I am getting the text output simultaneously". I have implemented the speech-to-text part in Python using the Google cloud service API. Then I used "child process" to run my python program in the node.js environment. Till now everything is fine. Next, I want to display the real-time text in the browser. In another word, I want to send the real-time text output from the python (which is now running in node.js using the child process) to the web browser. I was trying to do that with socket.io. Here is my server side (node.js) code where socket.io is also applied:
const express = require('express');
//const router = express.Router();
const {spawn} = require('child_process');
const path = require('path');
const app = express();
const http = require('http');
const server = http.createServer(app);
//const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
function runScript(){
return spawn('python3', [
"-u",
path.join(__dirname, 'script.py')
]);
}
const subprocess = runScript()
// print output of the script
app.get('/', (req,res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
subprocess.stdout.on('data', (data) => {
//console.log(`data:${data}`);
socket.on('message', (data) => {
socket.broadcast.emit('message', data);
});
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
Above, I am first using the child process to call the python program in node.js and then I am using socket.broadcast.emit to send the text output of the python program to my client side. The client-side code looks like this:
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var messages = document.getElementById('messages');
//const EventEmitter = require('events');
//const emitter = new EventEmitter()
//emitter.setMaxListeners(50)
socket.on('messages', function(data) {
document.querySelector("#style1".innerHTML = `<p>${data1}</p>`
});
</script>
</head>
<body id="messages">
<h1> This is crazy </h1>
<div id="style1">
</div>
</body>
</html>
Above, I want to display the real-time text output from the python program inside the <p> tag.
The problem is, I am not able to get anything in the web browser.
My objective is, I want to display whatever I am speaking as text in the web browser in real-time.
I don't know much about socket.io. In fact, this is the first time I am using this technology.
Your Node.js server will act as the socket server. As your code shows, it listens on a port for a socket connection, and on connection, creates a socket, which you then send messages too. From a simple cursory review, the server code looks okay.
On your webpage, you are creating the socket, and listening for messages.
However the socket running on the webpage hasn't yet connected to the server, which is why nothing is working yet.
Assuming you're doing this on localhost, just add the socket server address to it's constructor, and then listen for connect.
const socket = io('ws://localhost:3000');
socket.on('connect', () => {
// do any authentication or handshaking here.
console.log('socket connected');
});
More advanced implementations should gracefully handle closing sockets.
Per the following comment:
I added the lines you suggested above. Even now nothing is visible on the webpage but I am getting this warning: (node:14016) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit
Looking more closely at your server code, I believe this is the root issue
subprocess.stdout.on('data', (data) => {
//console.log(`data:${data}`);
socket.on('message', (data) => {
socket.broadcast.emit('message', data);
});
});
});
Each time you receive data from subprocess.stdout, you are adding a new onmessage event handler to your socket, so after a period of time, you have added too many event handlers.
Re-write your logic so that you only add socket.on('message') once (usually after your create the socket).
It is also worth noting that in the above code, data from stdout is not being used, as that data variable is being redefined in a lower scope by your onmessage function. Since data is being redefined, the output of your Python program is being ignored.
I think this is what you want:
//echo any message you receive from the socket back to the socket
socket.on('message', (data) => {
socket.broadcast.emit('message', data);
});
//send data from std out to the socket.
subprocess.stdout.on('data', (data) => {
//console.log(`data:${data}`);
socket.broadcast.emit('message', data);
});

Socket.io only emitting uppercase events

so I wanted to try out the socket.io library and all the examples work perfectly fine (with lowercase emits). But when I try to code my one little ping->pong it doesnt emit the events (I can view the message log in firefox network tab).
Code Server (Node JS):
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 9000;
app.use(express.static(__dirname + "/public"));
io.on("connection", (socket) => {
console.log("Socket connected");
socket.on("ping", () => {
console.log("PING");
socket.emit("pong", {});
});
});
http.listen(port, () => console.log("listening on port " + port));
Code in Browser:
var socket = io();
socket.on("pong", () => {
console.log("recieved PONG");
});
const ping = () => {
socket.emit("ping");
};
document.addEventListener("mousedown", ping, false);
Strangely, this doesn't seem to work, "Socket connected" is printed, but the sockets don't emit anything. If I change the emits and on's from "ping"->"PING" and from "pong"->"PONG" everything works perfectly fine. Im just totally confused to why this is and why the examples can use lowercase emits.
Since there is nothing related with UPPERCASE/LOWERCASE event names, you may use them as you wish.
But ping/pong actually uses by socket.io server with several of them. you can see the list here. It's on bottom of the page =)
So unless you respest these you can use upper/lower case event/room names.
Also those event's are listenable by user too.
io.on('connect', onConnect);
function onConnect(socket) {
socket.on('error', onError);
socket.on('disconnect', onDisconnect);
// ... and others too.
// You can see and console on ping/pong events too.
socket.on('ping', console.log);
socket.on('pong', console.log);
}
I know socke.io's documentation really is not the best :D
By the way, ping and pong usage is coming from ws which also used by socket.io internally. If you want to see more about i'll leave links here where you can see ping and pong events emitting.
Sender.js: ping also Receiver.js: ping & pong

Categories