Reputation: 790
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.
Upvotes: 3
Views: 5337
Reputation: 2341
One thing that you can do, is simply ping the Node.js server when there's an update with the details. You can do this over straight http/https.
Basically, when .NET updates the db, it can fire off a quick POST to a node.js endpoint with the data package that you want to roll out to users.
Upvotes: 1