Reputation: 377
I have two devices. One devices send data to one socket port ie. 9000 and another devices connected to another port ie. 8000 and I want 2nd device receive same data that is received on port 9000 from first device.
I want to make a script using nodejs and socket io to receive data from one port like: 192.168.25.42:9000
and same received data should be available on different port 192.168.25.42:8000
any help appreciated.
Upvotes: 1
Views: 3022
Reputation: 40434
You'll need to have 2 socket.io servers, one listening on port 9000 and the other one listening on port 8000.
When one server receives data, you emit that data to the other one.
const express = require("express");
const http = require('http');
const app8000 = express();
const app9000 = express();
const server8000 = http.Server(app8000);
const server9000 = http.Server(app9000);
const io8000 = require('socket.io')(server8000);
const io9000 = require('socket.io')(server9000);
server8000.listen(8000);
server9000.listen(9000);
// Server on port 8000
io8000.on('connection', socket => {
socket.on('my-event', data => {
//Push data to clients connected to server on port 9000
io9000.emit("event-from-8000", "From 8000: " + data);
});
});
// Server on port 9000
io9000.on('connection', socket => {
socket.on('my-event', data => {
//Push data to clients connected to server on port 8000
io8000.emit("event-from-9000", "From 9000: " + data);
});
});
Upvotes: 1