0

I have a windows application (Built on C# as windows service) that sends data to NodeJs Net Socket, So since Socket.IO helps making a Web Application a live one , without the need of reload. How can i allow Socket.IO stream the received data from NodeJs Net Socket to the Web Application , in the exact moment the Net Socket receives data from C#?

So in the code that receives the socket data from C#:

var net = require('net');
    net.createServer(function (socket) {
        socket.on('data', function (data) {
                broadcast(socket.name + "> \n" + data + " \n", socket);
                socket.end("<EOF>");

                //send data to web interface , does it work that way?
                //SomeFooToSendDataToWebApp(Data)
        });
   });

Further more for the Socket.IO i have those lines , which i cant really figure out how to deal with them:

//Should it listen to net socket or web socket?
server.listen(8080);

// Loading socket.io
var io = require('socket.io').listen(server);

// It works but only for one request
io.sockets.on('connection', function (socket2) {
    socket2.emit('message' , 'Message Text');   
});

P.S: I am new to nodejs & socket.io , so if its possible as well to explain their behavior.

Edit 1 : My Front End Javascript to check it if it has any problems:

  //for now it listens to http port , which Socket.IO listens to
  var socket = io.connect('http://localhost:8080');
            var myElement = document.getElementById("news");
            socket.on('message', function(message) {
                document.getElementById("news").innerHTML  = message;
            })

Edit 2 : Did follow jfriend00's answer as it seems my previous code tries were trying to send messages to an unknown socket, i only added this since i needed it to be sent to all the connected clients , so only one line fixed it !

 socket.on('data', function (data) {
                broadcast(socket.name + "> \n" + data + " \n", socket);
                socket.end("<EOF>");

                //send data to web interface , does it work that way?
                //The Added code here:
                io.emit('message',data + " more string");

        });

3 Answers 3

1

It's a bit hard to tell exactly what you're asking.

If you have some data you want to send to all connected socket.io clients (no matter where the data came from), then you can do that with:

io.emit("someMessage", dataToSend);

If you want to send to only one specific connected client, then you have to somehow get the socket object for that specific client and then do:

socket.emit("someMessage", dataToSend);

How you get the specific socket object for the desired connected client depends entirely upon how your app works and how you know which client it is. Every socket connection on the server has a socket.id associated with it. In some cases, server code uses that id to keep track of a given client (such as putting the id in the session or saving it in some other server-side data). If you have the id for a socket, you can get to the socket with the .to() method such as:

io.to(someId).emit("someMessage", dataToSend);

Your question asked about how you send data received from some C# service over a normal TCP socket. As far as sending it to a socket client, it does not matter at all where the data came from or how you received it. Once you have the data in some Javascript variable, it's all the same from there whether it came from a file, from an http request, from an incoming TCP connection in your C# service, etc... It's just data you want to send.

Sign up to request clarification or add additional context in comments.

8 Comments

Well to be more precise , my nodejs receives the socket message from C# perfectly , but since the only way to make a live web application is Socket.IO , how can i send the message upon receive from C# to the Web Application? i will edit my question to make it more clear.
@AnwarFazza - Your web application has to previously have connected to your server using socket.io.
Its true , but when i send new data to NodeJs , the data on web app never changes , even though its connected.
@AnwarFazza - Well, you'd have to show us both the server code that sends the data to the client and the client code that receives it for us to have any idea what's wrong with that code. Can't comment on code we can't see.
@AnwarFazza - Where's the server-side code for sending the data to the client?
|
1

You can try the following, simple server:

const io = require('socket.io')(8080);

io.on('connection', socket => {
    console.log('client connected');
    socket.on('data', data => {
        io.emit('message', data);
    });
});

console.log('server started at port 8080');

It should work if I understand the problem correctly.
And maybe document.getElementById("news").innerHTML += message; in the html client code to see what really happens there?

1 Comment

for me io.emit('message', data); , has to be in the part when the Net socket receives the data. indeed it did work as io.emit rather than socket.emit , because i needed to send it to all clients rather than each socket separated.
1

socket2 means your client which just connected. So you can store these connections to send data to them (helpful for broadcast).

If you get data from windows service via some polling mechanism, on this step you can send this message to your connected clients. So keep your connections in a array to send specific messages each client afterwards

1 Comment

I will try this concept now , and will give you the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.