I am using websocket and http server together, assigning each of them a different port, when a post request hits it sends a message to client using websocket and response of http request is send when client responds to the message.
My problem is I want to create multiple websockets but a single https server, I have tried some things but unable to do it.
Current code for creating websocket and httpserver is
const app = require('express')()
bodyParser = require('body-parser')
app.use(bodyParser.json())
const net = require('net');
var client;
var res1,currentReq;
//----------------------------------------------------------------------------
// http requests listener
//----------------------------------------------------------------------------
app.listen(8001, () => console.log('Http server listening on port 8001'));
//----------------------------------------------------------------------------
// http requests handling
//----------------------------------------------------------------------------
app.post('/getUser', (req, res) => {
console.log(req.body)
res1 = res;
currentReq='getUser';
client.write('{"route":"/","data":{"username":"' + req.body.username +'"}, "res": "' + res + '"}');
//res.end('{"Extension":"' +' data '+ '"}');
console.log("/getUser finished");
// res.end('{"Extension":"' +' data '+ '"}');
});
//----------------------------------------------------------------------------
// Establishing tcp connection for incoming requests
//----------------------------------------------------------------------------
var server = net.createServer(function(connection) {
console.log ('client has connected successfully!');
client = connection;
client.on('data',function(data){
switch(currentReq)
{
case 'getUser' :
console.log('send get user response');
res1.end(data);
break;
}
console.log(data.toString());
//res1.end(data);
});
connection.pipe(connection);
});
//----------------------------------------------------------------------------
// listener for tcp connections
//----------------------------------------------------------------------------
server.listen(8000, function() {
console.log('server for localhost is listening on port 8000');
console.log('server bound address is: ' + server.address ());
});
Related
I'm integrating websockets with express on the backend and using browser's native websocket api on the client side. I have so far been able to send and receive message from the client to server and server back to client. But all this happens with a page refresh only. Isn't websocket supposed to be real time? Lets say I make a change in the message on server file, then it has to immediately reflect in my browser's console. and lets say I make a change in the message in the script file on the client side, then it has to immediately show the changes on server's console.(Also I'm using nodemon to run the server so changes has to reflect pretty quickly). But right now, I see myself making a request to / via page refresh and then server upgrading and then responding back with the message.
Please tell me if I'm missing something in the code or otherwise in the concept?
app.js
const express = require('express')
const app = express()
const path = require('path')
const WebSocketServer = require('websocket').server;
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, '/public')))
const port = 8080
app.get('/', (req, res) => {
res.render('index')
})
const server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
wsServer = new WebSocketServer({
httpServer: server
});
function originIsAllowed(origin) {
return true;
}
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
var connection = request.accept(null, request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.sendUTF("server says hi");
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
client.js:
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send('Hello to Server!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
});
I am not sure if I understand the question, but if you want the client to send message to server, you have do it the same way as it is done in open listener:
socket.send('MESSAGE FOR SERVER') or, if server should send something to client, then just
connection.send('MESSAGE FOR CLIENT').
Realtime communication with WebSocket means, the connection is created only once and the protocol is kept opened (Unlike REST API, where the connection is created with every request.)
The message must be sent actively either from server or client, there is nothing observing some message state and updating it on the other side.
ı want to get data from tcp port and show data on clientside by Javascript or Angular.
ı can list data in console but dont know how to list on client side.
const port = 4000;
const host = '0.0.0.0';
let buffered='';
const client = new Net.Socket();
client.connect({ port: port, host: host }), function() {
console.log('TCP connection established with the server.');
client.write('Hello, server.');
};
client.on('data', function(chunk) {
console.log('rocket name :'+chunk.slice(1,11).toString(),'rocket altitude:'+chunk.slice(13,17).readFloatBE(0)
,'rocket speed:'+chunk.slice(17,21).readFloatBE(0)
,'Temprature :'+chunk.slice(29,33).readFloatBE(0));
client.end();
});
client.on('end', function() {
console.log('Requested an end to the TCP connection');
});
I am trying to build/deploy a tracker server for use with P2P applications using the below code. It works fine locally, but when I deploy it to Heroku, the port bindings fail because only one port is allowed.
// Create a web sockets signaling server
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//Allow all requests from all domains & localhost
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET");
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
let lookup = {}
const hostname = '0.0.0.0';
const port = process.env.PORT;
var Server = require('bittorrent-tracker').Server
var server = new Server({
udp: false, // enable udp server? [default=true]
http: true, // enable http server? [default=true]
ws: true, // enable websocket server? [default=true]
stats: true, // enable web-based statistics? [default=true]
})
server.on('error', function (err) {
// fatal server error!
console.log(err.message)
})
server.on('warning', function (err) {
// client sent bad data. probably not a problem, just a buggy client.
console.log(err.message)
})
server.on('listening', function () {
// fired when all requested servers are listening
console.log('Signal server http port:' + server.http.address().port)
console.log('Signal server ws port:' + server.ws.address().port)
})
// start tracker server listening! Use 0 to listen on a random free port.
server.listen(port, hostname, 'listening')
// listen for individual tracker messages from peers:
server.on('start', function (addr) {
console.log('got start message from ' + addr)
Object.keys(server.torrents).forEach(hash => {
lookup[server.torrents[hash].infoHash] = server.torrents[hash].peers.length
console.log("peers: " + server.torrents[hash].peers.length)
})
})
server.on('complete', function (addr) {})
server.on('stop', function (addr) {})
app.get('/peers', function(req, res) {
res.send(lookup);
})
app.listen(process.env.PORT, function() {
console.log('Express server port: ' + this.address().port); //Listening on port #
})
If I use process.env.PORT for both server and app, I get the following, which is expected since Heroku only allows 1 listen port:
2021-02-13T05:35:31.016101+00:00 heroku[web.1]: State changed from starting to up
2021-02-13T05:35:30.885170+00:00 app[web.1]: Express server port: 9898
2021-02-13T05:35:30.885204+00:00 app[web.1]: listen EADDRINUSE: address already in use 0.0.0.0:9898
2021-02-13T05:35:30.885205+00:00 app[web.1]: listen EADDRINUSE: address already in use 0.0.0.0:9898
If I hard code the port for either server or app, the application launches fine, but the signaling server doesn't work. No substantial logging is generated.
2021-02-13T05:38:21.141806+00:00 heroku[web.1]: State changed from starting to up
2021-02-13T05:38:20.998054+00:00 app[web.1]: Express server port: 25702
2021-02-13T05:38:20.998550+00:00 app[web.1]: Signal server http port:31415
2021-02-13T05:38:20.998683+00:00 app[web.1]: Signal server ws port:31415
Is it possible that the bittorrent-tracker server and express server can use the same port? For instance, could I get and return the list of peers within this block of code without the need for express at all?
server.on('start', function (addr) {
console.log('got start message from ' + addr)
// Could I do something here to eliminate the need for Express?
Object.keys(server.torrents).forEach(hash => {
lookup[server.torrents[hash].infoHash] = server.torrents[hash].peers.length
console.log("peers: " + server.torrents[hash].peers.length)
})
})
The documentation states:
The http server will handle requests for the following paths:
/announce, /scrape. Requests for other paths will not be handled.
But perhaps there is some way I can shim in the requests that express is handling?
Not long after asking this question, it occurred to me that I might not need express at all. It turns out that was correct.
For anyone wanting a Heroku-ready bittorrent-tracker, here is the updated code:
// Create a web sockets signaling server
let lookup = {}
const hostname = '0.0.0.0';
const port = process.env.PORT;
var Server = require('bittorrent-tracker').Server
var server = new Server({
udp: false, // enable udp server? [default=true]
http: true, // enable http server? [default=true]
ws: true, // enable websocket server? [default=true]
stats: true, // enable web-based statistics? [default=true]
})
server.on('error', function (err) {
// fatal server error!
console.log(err.message)
})
server.on('warning', function (err) {
// client sent bad data. probably not a problem, just a buggy client.
console.log(err.message)
})
server.on('listening', function () {
// fired when all requested servers are listening
console.log('Signal server http port:' + server.http.address().port)
console.log('Signal server ws port:' + server.ws.address().port)
})
// start tracker server listening! Use 0 to listen on a random free port.
server.listen(port, hostname, 'listening')
// listen for individual tracker messages from peers:
server.on('start', function (addr) {
console.log('got start message from ' + addr)
Object.keys(server.torrents).forEach(hash => {
lookup[server.torrents[hash].infoHash] = server.torrents[hash].peers.length
console.log("peers: " + server.torrents[hash].peers.length)
})
})
server.on('complete', function (addr) {})
server.on('stop', function (addr) {})
I am using http server with socket connection response of http request goes when client responds to socket connection i am currently doing it by storing res in a global object which i think is not a correct method, what is the correct method to do it.
const app = require('express')()
bodyParser = require('body-parser')
app.use(bodyParser.json())
const net = require('net');
var client;
var res1,currentReq;
//----------------------------------------------------------------------------
// http requests listener
//----------------------------------------------------------------------------
app.listen(8001, () => console.log('Http server listening on port 8001'));
//----------------------------------------------------------------------------
// http requests handling
//----------------------------------------------------------------------------
app.post('/makeCall', (req, res) => {
console.log('sd' + req.body)
res1 = res;
currentReq='makeCall';
client.write("something");
});
//----------------------------------------------------------------------------
// Establishing tcp connection for incoming requests
//----------------------------------------------------------------------------
var server = net.createServer(function(connection) {
console.log ('client has connected successfully!');
client = connection;
client.on('data',function(data){
switch(currentReq)
{
case 'makeCall' :
res1.end(data);
break;
}
});
});
//----------------------------------------------------------------------------
// listener for tcp connections
//----------------------------------------------------------------------------
server.listen(8000, function() {
console.log('server for localhost is listening on port 8000');
});
Based on our discussion in comments, here is an example of how one might do this using socket.io.
Using a socket.io set-up, the browser will connect to the HTTP server to make HTTP GET AND POST requests, e.g. to /makeCall. The browser will also connect to the same HTTP server to open the socket. The browser will connect by HTTP initially, then send a special handshake which will convert the connection into a WebSocket.
I still am not sure to understand your use case so perhaps this solution is not appropriate, but here is the general setup of what it might look like.
const app = require('express')()
const bodyParser = require('body-parser')
const server = require('http').Server(app);
const io = require('socket.io')(server);
const cookieSession = require('cookie-session')
app.use(bodyParser.json())
app.use(cookieSession({
keys: ['secret key', 'another one', 'a third one'],
maxAge: 24 * 60 * 60 * 1000 // cookie max-age : 24 hours
}))
//----------------------------------------------------------------------------
// http requests listener
//----------------------------------------------------------------------------
server.listen(8001, () => console.log('Http server listening on port 8001'));
// WARNING: app.listen() will NOT work here! per socket.io docks
const idToSocketMap = new Map();
//----------------------------------------------------------------------------
// http requests handling
//----------------------------------------------------------------------------
app.post('/makeCall', (req, res) => {
console.log('sd' + req.body)
if (!req.session.id) {
req.session.id = generateId();
}
//this may not be necessary, see below
req.session.currentReq = 'makeCall';
const socket = idToSocketMap.get(req.session.id)
if (socket) { //if a socket connected for that id
socket.emit("makeCall", "an argument/parameter", "something");
}
});
//----------------------------------------------------------------------------
// Establishing websocket connection for incoming requests
//----------------------------------------------------------------------------
io.on('connection', (socket) => {
//"socket" is now what would have been called "client" in old code
//get the id from the session cookie of the http request
const id = socket.request.session.id;
//put the socket in the map
idToSocketMap.set(id, socket);
//remove the socket from the map on disconnect
socket.on('disconnect', () => idToSocketMap.delete(id));
console.log('client has connected successfully!');
socket.on('makeCall', (firstParameter, something) => {
// It's still not possible to send anything back to an HTTP connection
// but we may do some things and reply here
const someData = getSomeData();
socket.emit('makeCallResponse', 'someData');
});
});
I have below source code about socket server/client in nodejs.
const net = require('net');
net.createServer((socket)=>{
socket.pipe(socket);
socket.on('data', (data)=>{
console.log('receive data ', data.toString());
socket.write('this is server')
});
}).listen(3000);
console.log("Chat server running at port 5000\n");
let client = new net.Socket();
client.setEncoding('utf8');
client.connect(3000, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
when I run above code, the server will send the data(which is from the client) back to the client. I know this is cased by socket.pipe(socket);. I want to keep that code and am looking for a way to separate the input/output data in client side like below:
client.on('data', function(data) {
if( the data is sent from client ){
...
} else {
//this is the data generated from server
}
});
Does anyone know how to do that in nodejs client side?
Actually you can separate out the data from client and server, depending upon what messages you emit or what you listen, from that you can distinguish between client and server.
The message you emit from server are the message, you received on the client side if client is listening to that messages, the message you emit by client are the message received on server if server is listening to the messages.
Below is the code for a socket.io
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.send('<h1>SocketChat Server</h1>');
});
http.listen(3000, function(){
console.log('Listening on *:3000');
});
io.on('connection', function(clientSocket){
/* Here server is listening to client 'connectUser' message so here data is sent from client*/
clientSocket.on('connectUser', function(clientNickname, groupName) {
var message = "User " + clientNickname + " was connected." + clientSocket;
console.log(message);
clientSocket.emit('userConnected');
});
});
/* Here server is emitting message('userConnected') that will be received by client. */
From above you can see that how to distinguish the client and server messages.