Websocket between two computers on same LAN network - websocket

Is it possible to open a websocket between two computers that are either on the same WAN network or connected via an ethernet cable (LAN)? If it is, can someone provide with some readings I can do to learn about this?
EDIT: added my server code
'use strict';
var express = require('express'); //web server
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server); //web socket server
// ===============================Serial port setup========================
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var parsers = serialport.parsers;
var openSocket = false;
var port = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: parsers.readline('\r\n')
});
// ===============================Server Setup===========================
const netPort = 8080;
server.listen(netPort); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains
the static webpages
console.log("listening on port: localhost:" + netPort);
// ==============================Sending data======================
io.sockets.on('connection', function (socket) {
socket.emit('pi', { status: 'opened' });
console.log('Socket is open'); // log to console, once serial connection is established
openSocket = true;
});
port.on('data', function(data) {
if (openSocket) {
socket.emit('pi', {value: data}); //send data
}
});

Related

Can't send a message from server to client with socket.io

I'm new to socket.io and was trying to send a message from server to client while following the instructions of basic emit on https://socket.io/docs/v4/emitting-events/. I expected when I connected to the socket, I would have the word 'world' printed on the console but I failed without knowing why. Did I do something wrong?
Server
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
io.emit('welcome', 'world');
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});
Client
var socket = io();
socket.on('welcome', (arg) => {
console.log(arg);
}
After io.on('connection') You have to receive message from client using
socket.on('welcome',(data) {
//from there emit to client receive message
}) ;

socket.io client not able to connect and receive messages emit by server

I am emitting messages from socket.io server running on port 8001
but my socket.io client not able to connect and receive these messages
my index.html (client):
<script src="https://cdn.socket.io/socket.io-4.0.0.js"></script>
<script>
//var socket = io();
//var socket = io.connect('http://localhost:8001');
var socket = io('http://localhost:8001', { transports : ['websocket'] });
socket.on('connect', function(){
console.log("connected");
socket.on("message", data => {
console.log(data);
});
});
</script>
My nodejs server code:
const app = require("express")();
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: '*',
}
});
io.on("connection", () => {
console.log("Connected!");
});
var redis = require('redis');
//var url = "redis://:#localhost:6379";
//var redis = require('redis-url').connect();
//var client = redis.createClient(url);
var client = redis.createClient();
//var client = redis.createClient();
client.on("error", function(error) {
console.error(error);
});
client.subscribe('notification');
client.on('message', function(channel, msg) {
console.log("Message received: "+msg);
io.sockets.emit(msg);
});
console.log('starting server on 8001...');
server.listen(8001);
My node js server console logs:
starting server on 8001...
Message received: from laravel
io.sockets.send(msg);
this worked for me. also make sure you are using the same version of socket.io on both client and server

get socket value from outside(socket.io)

let app = require('express')();
let server = require('http').Server(app);
let io = require('socket.io')(server);
io.on('connection', function (socket) {
// I want to get this value from outside
})
I want to know how to use value socket from ourside .. I have to use socket.emit() in another function
const WebSocket = require('html5-websocket');
const agent_websock = new WebSocket('ws://ip:post/');
agent_websock.onmessage = function (m) {
let msg = JSON.parse(m.data);
console.log(msg, 'agent');
socket.emit('news', {msg: msg});
};
You can get socket value from outside by the following way
==>app.js
var express = require('express');
var socket = require('./socketServer');
var app = express();
var server = app.listen(3000, function () {
console.log('Listening on port 3000 ...');
});
socket.socketStartUp(server);
module.exports = app;
==>helper.js
var helperFunction = {}
helperFunction.emitMessage = function (socket) {
socket.emit('event_name',{'key':'value'})
}
module.exports = helperFunction;
==>socketServer.js
var io = require('socket.io')();
var helper = require('helper');
var socketFunction = {}
socketFunction.socketStartUp = function (server) {
io.attach(server
io.on('connection', function (socket) {
console.log("New user is connected with socket:", socket.id);
helper.emitMessage(socket);
})
}
module.exports = socketFunction;
Hope this answer is solved your query

Can't use Express to send data back to client more than once

In my app, I send a post request to the server with data containing a CSV file:
$.ajax({
type:"POST",
contentType: "application/json",
url:"/",
data: JSON.stringify({fileData:My_CSV_FILE}),
success: function(csvJson) {
console.log('in the done block!');
//can use csvJson in this handler
});
});
Note: I'm posting to the home route, and I am able to get a response with the data converted from the server. The problem is that whether I run on localhost or Heroku, I am only able to trigger the POST request once, then I have to restart the server (even if I refresh the page). So I know the issue is with my route somewhere:
UPDATED TO INCLUDE FULL SERVER FILE:
'use strict';
const express = require('express');
const csvtojson = require('csvtojson');
const PORT = process.env.PORT || 3000;
const bodyParser = require('body-parser');
const Converter = require('csvtojson').Converter;
var converter = new Converter({});
let app = express();
app.use(bodyParser.json({limit: '300kb'}));
app.use(express.static(__dirname +'/public'));
app.post('/',function(req,res) {
var csvFile = (req.body.fileData);
converter.fromString(csvFile, function(err, result) {
if(!err) {
console.log(result);
res.json(result);
}else {
res.json({error: 'Could not convert'});
}
})
});
app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`);
});
I'm using Express 4. Again, everything works, but only once. When I run Heroku logs, or check the console on localhost I get:
Error: Can't set headers after they are sent.
But I don't understand how I'm re-setting them.
If wanting to run on localhost, here is a link to the projects github: https://github.com/qctimes/calendar_export
You should move the converter instantiation to be done inside the app.post callback method. This way it will instantiate a new object at every request.
This is is how your code should be:
'use strict';
const express = require('express');
const csvtojson = require('csvtojson');
const PORT = process.env.PORT || 3000;
const bodyParser = require('body-parser');
const Converter = require('csvtojson').Converter;
let app = express();
app.use(bodyParser.json({limit: '300kb'}));
app.use(express.static(__dirname +'/public'));
app.post('/',function(req,res) {
var csvFile = (req.body.fileData);
var converter = new Converter({}); // instantiation is done here
converter.fromString(csvFile, function(err, result) {
if(!err) {
console.log(result);
res.send(result);
}else {
res.send({error: 'Could not convert'});
}
});
});
app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`);
});

Easyrtc data exchange through websockets

Think I must be missing something, but I don't have the debug capability to check. Web app passes data to node.js server through socket.io, but doesn't emit anything back.
server.js
// Load required modules
var http = require("http"); // http server core module
var express = require("express"); // web framework external module
var io = require("socket.io"); // web socket external module
var easyrtc = require("easyrtc"); // EasyRTC external module
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
var httpApp = express();
httpApp.use(express.static(__dirname + "/static/"));
// Start Express http server on port 8080
var webServer = http.createServer(httpApp).listen(8080);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer, {"log level":1});
// Start EasyRTC server
var rtc = easyrtc.listen(httpApp, socketServer);
easyrtc.events.on('get_candy', function(easyrtc) {
easyrtc.events.emit('did it');
});
file.js
function joined_room() {
easyrtc.sendServerMessage('get_candy', {candy_name:'mars'},
function(msgType, msgData ) {
console.log("got candy count of " + msgData.barCount);
},
function(errorCode, errorText) {
console.log("error was " + errorText);
});
}
easyrtc.setServerListener( function(msgType, msgData, targeting) {
console.log("The Server sent the following message " + JSON.stringify(msgData));
}, 'get_candy');
I got same problem few months ago and now my solution is reuse websocket on server:
In client side:
var socket = io.connect('http://' + document.domain + ':' + location.port);
easyrtc.useThisSocketConnection(socket);
In server side:
var io = require("socket.io"); // web socket external module
var easyrtc = require("easyrtc"); // EasyRTC external module
var httpApp = express();
httpApp.use(express.static(__dirname + "/static/"));
// Start Express http server on port 8080
var webServer = http.createServer(httpApp).listen(8080);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer);
var rtc = easyrtc.listen(webServer, io, null, function(err, rtcRef) {
rtcRef.events.on('roomCreate', function(appObj, creatorConnectionObj, roomName, roomOptions, callback) {
console.log('roomCreate fired!');
});
});
This snip allows you reuse your websocket for easyRTC. Signature message for RTC and other can using same websocket.

Resources