I am using socket.io with nodejs. So I want to broadcast to all clients from URL..
For Example:
My nodejs Server : www.domain.com:7979
var io = require('socket.io').listen(7979);
io.sockets.on('connection', function (socket) {
socket.on('addRow', function (data) {
console.log(data);
io.sockets.emit('newRow', data);
});
});
And This is my client code:
client.php
$(function(){
var socket = io.connect('http://www.semtr.com:7979');
socket.emit('addRow',{taskId:<?php echo $id ?>});
//socket.disconnect();
});
</script>
As I called my client.php vith console, My javascript function didnt run..
I want to call nodejs function with url like this.
http://www.domain.com:7979/addRow
or
call this javascript function vith shell
socket.emit('addRow',{taskId:<?php echo $id ?>});
Thanks.
If you emit on the server, you have to listen to the results on the client, and the other way around:
server:
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
client:
socket.on('news', function (data) {
console.log(data);
});
Like patrick said you should have a listener in client side. Another thing, did you get anything on the server? did it receive data? You didn't specify what part didn't run. Remember you can connect with just io.connect() if your server is listening same port as your socket.io module, possibly a mismatch too, be more specific please.
`$(function(){
var socket = io.connect();
socket.emit('addRow',{taskId:<?php echo $id ?>});
//socket.disconnect();
socket.on('newRow', function (data) {
//do something with server's message
console.log(data);
});
});`