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
});
});
Related
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');
I have nodeJs application which is running on server http://mysite.co:8081. Where I have chat application. Using socket.io for communication.
Now I want to connect to socket from localhost -- I am trying to connect from my machine where I have wamp installed and in that created one html file in www/test/index.html.
The index.html have below code only.
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
The socket.io.js file is loading correctly, But I am getting below error
Uncaught TypeError: io is not a function
Let me now what I am doing wrong.
Thanks,
try this one:
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
My socket.on code does not work on the clientside, but on the serverside.
I have 2 pages, one to socket.emit(so I can call it whenever I want to) and one (the main/index page) to listen for the emit socket.on.
Servercode:
var io = require('socket.io').listen(app.listen(8080));
io.on('connection', function (socket) {
socket.on('playerData', function(data) {
console.log(data);
});
});
Clientcode:
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var socket = io('http://localhost:8080');
socket.on('playerData', function(data) {
console.log(data);
});
</script>
I am using Node.JS - Jade, Express, Socket.io (and AngularJS, but not in this part of the code)
As already mentioned, the Serverside console.log is being called when I call my emit function, but on the Clientside nothing happens.
Currently using Chrome as my browser.
I know this error message has been asked in lots of question but I haven't found one that matches my situation. Below I show you the server (node.js) and the client code. The socket.io.js file is included and definitely present.
This problem occurs if you are not listening to the correct port.
Server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:80');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
If the server and the client is not listening to the same port, then you will not be able to load the socket.io library and io will be undefined. I'm guessing that you did not specify the port on the client when connecting to a different domain. If you are using express, the port number should be the port Express listens to by default.
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);
});
});`