Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a node.js-app running on the same machine on port 8080 with different channels. The communication between my jQuery-site and my .NET endpoint works perfectly.

My Site:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
            <title>WebSocket-Test</title>
    </head>
    <body>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            $(function() {
                $('button').click(function() {
                    var socket = io.connect('http://localhost:4000');
                    socket.on($('#username').val(), function (data) {
                        console.log(data);
                        socket.emit('my other event', { my: data });
                    });
                });
            });
        </script>
        <input type="text" id="username" />
        <button>connect</button>
    </body>
</html>

My node.js-server:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(4000);
var count = 0;

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
    setInterval(function () {
        socket.emit('daniel', { hello: 'Waited two seconds!'});
    }, 2000);
    socket.emit('daniel', { hello: 'world' });  
    socket.emit('stefan', { hello: 'world2' });  
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

My question is, how can I emmit a message from my .NET backend via node.js?

After my page is loaded I do have a window.io-object. What is the best approach? Just do an eval on the io-object with emmit and the channel or can I pass an object or json-thing to my node.js-server?

My target is, to send an event-driven message. When a new row is inserted into my MSQL-DB a message should be send to the channels.

share|improve this question
Just by the way. StackOverflow allows you to post code directly, inclusive code highlightning. (This make testing something easier because you can copy and paste what was asked.) – TheHippo May 15 at 18:54
Thanks for your hint, I have edited my post. – daniel.didusch May 16 at 6:31
1  
Were you planning on having .NET poll the DB to publish the events? And are the insertions happening from other sources? Also, this question might help... – Jeff Bridgman Jun 7 at 13:22

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.