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>
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 written code for socket from :source
My code looks like this.
var port = 8081;
var app = require('http').createServer();
var io = require('socket.io')(app);
app.listen(port);
io.on('connection', function (socket) {
console.log("New user connected.");
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log("server listening on: " + port);
But when I am testing it on here (ws://localhost:8081). I am getting
ERROR: undefined
DISCONNECTED
In console I am getting error:
WebSocket connection to 'ws://localhost:8081/?encoding=text' failed: Connection closed before receiving a handshake response
You need to use the socket.io client library, either getting it, from here, or referencing their CDN from your page like this:
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
Additionally, whenever you start a socket.io server, socket.io will be served from http://<your-server-address>/socket.io/socket.io.js. You should be able to test your server using this simple page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://localhost:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8081/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
</body>
</html>
You can read more about why you can't connect from the websocket.org site on this github issue.
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.
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
});
});
I've set up a Ubuntu 10.04 server in my home network. I installed a lamp-server, NodeJs and Socket.io on it. It al seems to work fine but I can not make a connection from my client to the server. I think that I am missing the point of where to store the client files and/or how to the IP adress and port should be put in the code.
I'm using the example from David Walsh (http://davidwalsh.name/websocket). My ip-adress of the server is 192.168.1.113. On the client I store app.js in
/usr/local/bin
That is where node is installed as wel.
Server code
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// Start the server at port 8000
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(8000);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});
When I start the server with Node and enter the adress in a browser (http://192.168.1.113:8000) I see 'Hello Socket Lover!' so I guess this works fine.
I store my client file (client.html) in
/var/www
That is where the home folder of my LAMP server is.
Client.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Create SocketIO instance, connect
var socket = new io.Socket('http://192.168.1.113',{
port: 8000
});
socket.connect();
// Add a connect listener
socket.on('connect',function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
}
</body>
</html>
Now when I open this file in a browser (http://192.168.1.113/client.html) nothing happens. I see no connection on my Ubuntu server or other logs and there are no messages in the browser.
Ehmm... what am I doing wrong? I've tried for hours, changing all kind of things but still no result.
With the help of pmavik (see comments) my question is answered.
What I didn't know was how to serve the client file properly (index.html). I copied an example from www.socket.io, added the right ip-adress and port and now a connection is made.
The server file (app.js) and the client file (index.html) should be in the directory where Node is installed. The Node-server sends the client file to the browser so there is no need to put the client file in the /var/www directory where the 'normal' files for websites are.
app.js
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8000);
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);
});
});
index.html
<html>
<head></head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.113:8000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>