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.
Related
I am trying to use io.socket onto a node js application.
In server side:
io.on('connection', function(client){
client.on('event', function(data){console.log("I am here")});
client.on('disconnect', function(){});
});
In client side:
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
server.listen('80');
app.listen(port);
But I think I have some problem with the port because the port is defined under folder bin/www.
when i try to upload the code into heroku, the heroku app is broken down. It was working fine in localhost.
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>
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
});
});
Browser can't find socket.io.js for client:
<script src="/socket.io/socket.io.js"></script>
When server is created without handler:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(80);
//without this part:
/*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) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I don't need and don't want handler function because everything I generate in PHP. And sometimes use client application functions for another file than index.html/php
So how to make browser can find socket.io.js?
I've wrote a demo app that you could have a look at if you don't want to loose to much time getting started with socket.io and Express 3.
To have websockets working your client js needs to be delivered from a webserver. This is one of those many browser limitation.
The easiest setup is to have a node server that provide both the client side Js and the WebSockets. Using easier the http module of Express (a bit overkill but super practical if you want to build something more than just a test app).
Other wise you need to have your client side js pointing to the right place. For example if you run your socket.io server of port 8080 and you deliver your static client side on port 8000 (using python -m SimpleHTTPServer for example or port 80 using a regular apache).
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
If you don't need access to http module functionality use this way:
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);
});
});
Include this on your client side !
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
var io = io.connect();
I am using socket.io in nodejs and I am able to send data from client to server. But when I emit from server, the client does not seem to be receiving this...
What am i missing ?
server:
socket = io.listen(app);
socket.sockets.on('connection', function(client){
client.on('something-from-client', function(msg){
console.log(msg);
//do something.
client.emit('some-result',{"total":docs.length});
});
});
client:
var socket = io.connect('http://localhost:9999');
socket.on('some-result', function(data){
console.log('received from server', data);
});
socket.emit("something-from-client", {"lat":lat, "lng":lng});
Ok got it working, if express is being used, some minor changes need to be done on server side
var port = 8111;
var server = express.createServer();
io = require('socket.io').listen(server);
server.listen(port);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
As promised - Working code - https://github.com/parj/node-websocket-demo/tree/socket_emit
CF Reference - http://socket.io/#how-to-use