Reputation: 89
I have an application that receives messages from a program. This application receives the messages and in turn broadcasts the messages to the connected clients. Now i am displaying the messages on the console and on the console, this receiver application is receving perfectly. However on the client (the html page), it is not being broadcasted. When i open the localhost/result, nothing is displayed. What am i doing wrong?
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;
var connectionString = 'connection string';
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
var printError = function (err) {
console.log(err.message);
};
var result;
var printMessage = function (message) {
console.log('Message received: ');
result = JSON.stringify(message.body);
obj = JSON.parse(result);
console.log('message:' + result)
console.log('');
};
count =0;
app.get('/result', function(req, res){
res.sendFile(__dirname + '/result.html');
});
io.on('connection', function(socket){
console.log('user connected');
socket.on('chat message', function(msg){
io.emit('chat message', result);
console.log("This is id in the sock' section" + check_id);
});
socket.on('disconnect', function(){
console.log('user disconnected');
socket.removeAllListeners('disconnect');
io.removeAllListeners('connection');
});
});
var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
return partitionIds.map(function (partitionId) {
return client.createReceiver('$Default', partitionId, { 'startAfterTime' : Date.now()}).then(function(receiver) {
console.log('Created partition receiver: ' + partitionId)
receiver.on('errorReceived', printError);
receiver.on('message', printMessage);
});
});
})
.catch(printError);
http.listen(3000, function(){
console.log('listening on *:3000');
});
result.html
<html>
<head><title>Hello world</title></head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
// socket.emit('chat message')
socket.on('chat message',function(msg){
socket.emit('chat message',msg);
document.write('hello world');
document.write(msg);
});
</script>
</html>
Upvotes: 1
Views: 326
Reputation: 6211
I would directly send it from the printMessage
function :
function printMessage(message) {
result = JSON.stringify(message.body);
console.log('[message]',result);
io.sockets.emit('chat message',result);
}
and remember to modify your client (result.html
) so that it doesn't make an infinite loop :
<script>
var socket=io();
socket.on('chat message',function(msg){
console.log(msg); //alert(msg);
document.write(msg);
});
</script>
EDIT :
How are you including the socket.io.js script?
<script src="/socket.io/socket.io.js"></script>
Try and specify the server address socket.connect('localhost:3000');
I made this working example for you
Upvotes: 1