How to call nodejs event from URL or Shell - node.js

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);
});
});`

Related

socket.io emit to one client not working

I'm using socket.io 1.7.3 version. Server code:
io.on('connection', function (socket) {
console.log(socket.id); // CdnNBVe9ktJmMcb1AAAA
socket.to(socket.id).emit('something');
socket.emit('something'); // if I do it without to, it works but to all clients
console.log(socket.rooms); // { CdnNBVe9ktJmMcb1AAAA 'CdnNBVe9ktJmMcb1AAAA' }
});
Client:
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect(..);
socket.on('connect', function() {
console.log(socket.id); // CdnNBVe9ktJmMcb1AAAA
socket.on('something', function() {
alert('it works');
});
});
Why it doesn't work? I'm not getting any alert although all console.logs seems to be correct.
To send a message to the particular client, you must provide socket.id of that client to the server and at the server side socket.io takes care of delivering that message by using,
socket.broadcast.to('ID').emit( 'send msg', {somedata : somedata_server} );
In your code
socket.broadcast.to(socket.id).emit('something');

How to transmit socket.io emit events between different pages of a node application

I need send data from one page to another page using Socket.io in node. Both pages have different script. The pageA have the script fileA.js, and the pageB have the script fileB.js Here's my code:
fileA.js
$('canvas').on('mouseup', function() {
var dataCan = JSON.stringify(canvas);
socket.emit('upcanvas', dataCan);
});
and this the page that receives that data:
fileB.js
var socket = io.connect('http://localhost:3000');
socket.on('get-data', function(data){
console.log(data);
});
And Here's the server file that receive that data and send the events of socket:
server.js
//Sockets
io.sockets.on('connection', function(socket)
{
socket.on('upcanvas', function(data){
socket.emit('get-data', data);
});
});
But that don't work!, I tried separately fileA.js and fileB.js and the socket works perfectly, but when I try to combine the emit/on events between that two pages, don't occurs nothing. What's wrong in that code?
I've found the solution !, simply this:
//Sockets
io.sockets.on('connection', function(socket)
{
socket.on('upcanvas', function(data){
socket.broadcast.emit('get-data', data);
});
});

How to call functions in socket.io?

Hello guys this is my code in the server side of socket.io and I don't know how to call this functions in the client side. Can you help me or provide me the code on how to call this functions:
io.sockets.on('connection', function (socket){
sub.on('subscribe', function (channel) {
pub.publish('Privatex','Test Message 1');
pub.publish('Privatex','Test Message 2');
pub.publish('Privatex','Test Message 3');
});
sub.on('message', function (channel, message) {
console.log(channel + ':' + message);
sub.unsubscribe();
pub.end();
sub.end();
});
sub.incr('Channel Test');
sub.incr('Privatex');
});
Please consult the How-To-Use section first.
To invoke this code on client side, you simply have to use this code, assuming socket.io is already configured on server side.
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');//path to socket.io
</script>
But let' take two minutes to see what's going on : this will be invoked on EVERY client connection because it's directly under the connection event. It's sometimes not the best way to do this (because a client can be briefly disconnected), and it's better to emit events from the client.
client code
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');//path to socket.io
socket.emit('my other event', { my: 'data' });
</script>
server code
io.sockets.on('connection', function (socket) {
socket.on('my other event', function (data) {
//do something here
});
});

Sending a message to the client on connection using node and socket io

How do i send a message to a specific client, more specifically, the client that has just connected to the app without broadcasting to the rest of the visitors that are already on the site?
io.sockets.on('connection', function(client) {
});
Seems to broadcast to everyone every time a new visitors connects.
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('greetings', { greeting:'hello, new visitor' });
socket.on('greetingFromVisitor', function (data) {
console.log(data);
});
});

Why is my Socket.io Express app just sending to the 'sender's' client?

I'm trying to write a basic chat application with Node.js (Express), and Socket.io. Everything 'seems' to be working, but my socket server seems to be only 'sending' the message back to the original sender. Here is my socket code:
var client = io.listen(app);
client.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
console.log(data);
socket.send(data);
});
});
And here is my client side code:
$(document).ready(function() {
var socket = new io.connect('http://localhost:3000');
socket.on('connect', function() {
socket.send('A client connected.');
});
socket.on('message', function(message) {
$('#messages').html('<p>' + message + '</p>' + $('#messages').html());
console.log(socket);
});
$('input').keydown(function(event) {
if(event.keyCode === 13) {
socket.send($('input').val());
$('input').val('');
}
});
});
Help is appreciated.
Use client.sockets.emit instead of socket.emit. It will emit to every connected client (broadcast), using the socket object only sends to the specific client.
Server side, I think you want:
socket.broadcast.emit(data);
instead of:
socket.send(data);
See "Broadcasting Messages" at the bottom of the "How to use" page. :)

Resources