I'm trying to get this exmaple working on my own computer:
https://github.com/tamaspiros/simple-chat
I have node.js installed and have installed socket.io as well. The readme is informing me to change the IP addresses on these two lines:
var socket = io.listen(1223, "1.2.3.4");
var socket = io.connect("1.2.3.4:1223");
However, I'm not really sure what to change the IP addresses into. I would like to get the simple chat box application working on my own computer.
You just need to configure it in one like, for ie:
var socket = io.connect('adress:port');
where addres is Your socket.io server IP or hostname, and yyyy is port on which it listens.
after that, You can get and emit events with:
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
on server listen for
and thats it.
Before making chat You can try just using this example from official socket.io: link
PS dont forget to add socket.io client file source on client side! :). The whole code should look like in the example in the provided link.
Related
I am trying to write a text file from NodeJs. I have server running on my laptop. Currently, i am running client on my laptop and it works fine. But if i run same NodeJs client on Linux running on raspberrypi, it doesn't write on file or neither it gives any error.
I have the following code for client
var ioC = require('socket.io-client'),
ioClient = ioC.connect('http://localhost:4000'),
fs = require('fs'),
os = require('os');
ioClient.on('connect', function () { console.log("socket connected"); });
ioClient.on('ChangeState', function(msg){
console.log(msg);
fs.writeFile('server.txt', JSON.stringify(msg), function (err){
if (err) return console.log(err);
});
});
Can anybody please help me what can be the issue with this?
You are connecting to localhost which won't work if the client is on a different machine. You need to change the server to listen to the ip address your server has in your network, and also need to let your client connect to this ip. You can get the ip by running ifconfig in your terminal. Then (depending on wireless or wired connection) look for something like (usually the last paragraph):
and create the server on this ip. E.g.
192.168.178.30:4000
and connect to the same address from your client.
To find your ip on windows, refer to this guide
I want to use the net module from Node.js on the client side (in the browser):
var net = require('net');
So I looked up how to get Node.js modules to the client, and browserify seems to be the answer. I tried it with jQuery and it worked like a charm.
But for some reason the net module does not want to work. If I write require('jquery') it works fine, but if I write require('net') it does not work, meaning my bundled .js file is empty.
I tried to search for something else, but the only thing I found is net-browserify on Github. With this, at least my bundle.js file is filled, but I get a JavaScript error using this (it has something to do with the connect function).
This is my code which works on the server side just fine:
var net = require('net-browserify');
//or var net = require('net');
var client = new net.Socket();
client.connect({port:25003}, function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
I assume that net-browserify lets you use a specific connect function, but I don't know which.
How can I use the net module from Node.js on the client side?
This is because net gives you access to raw TCP sockets - which browsers simply cannot do from the JavaScript end. It is impossible for net to ever be ported to the client side until such an API is written (allowing arbitrary tcp traffic).
Your best bet if you want to send tcp data from the client to the server is using web sockets using the socket.io module or the ws one.
Your best bet if you want clients to communicate directly is to look into WebRTC
I need to access Web sockets via specific path, I mount my socket.io on the client with a path ('ws')
Server code:
var io = require('socket.io')(server, {path: '/notif'});
Client code:
var socket = io('//127.0.0.1:7733/ws/', {path: '/notif'});
socket.connect();
This does not work due to “ws” on client. I suspect it's because I don't have the equivalent on the server (e.g. require server on specific path).
(when removing the /rt mount, everything seems to work as expected).
What is the server api to set up ws to listen on specific URL ?
Are you sure you know what /ws/ in your url is used for?
Here you are asking to connect to the ws namespace.
To receive connection for that namespace on the server you have to write:
io.of('/ws').on('connection', function(socket){
console.log('someone connected');
});
See: http://socket.io/docs/rooms-and-namespaces/
Also you don't need to call socket.connect();
Calling io() or io.connect() will already try to establish connection with the server.
I've been playing around with socket.io (very much a newbie), my goal is to post data to the server, do a small database entry and return data to the user real time.
My default server is nginx currently.
I've been looking at the example on socket.io, but this is only when wanting to use node.js to handle everything.
When trying to listen on port 3000, I get a warning of:
server:
var io = require('socket.io').listen(3000);
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:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
I simply want to push the data from my web page to the node server, but whenever I do currently, I get a 404 for the socket.io/socket.io.js file. I've looked in to this, and other answers suggest having an update to date version of the node module express? I've ensured I have the latest versions of node.js, socket.io and express installed on my server.
Is there anywhere obvious where I'm going wrong here, sorry if this is an obvious question.
Assuming you're running a Linux machine, there are two problems. For one, Nginx will already be running on port 80. Therefore, you can't run another process on that port, and should either run Node.js on another port, or follow a setup like this.
The other problem is that you need root access to use the ports < 1024 on a Linux machine. Without proper permission, you will get the EACCES error you're seeing.
Once the server binds properly, the socket.io.js file will no longer return a not found error.
Can socket.io client connect to two different ports on the same server?
Can socket.io client connect to two different server?
Sure:
var io1 = require('socket.io').listen(8001);
io1.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
var io2 = require('socket.io').listen(8002);
io2.sockets.on('connection', function (socket) {
socket.emit('flash', { hello: 'world' });
});
Perhaps this is an interesting read: (from github)
// connect at the same host / port as your website
var socket = io.connect();
// different port or host
var socket = io.connect('http://url.com');
// secure
var socket = io.connect('https://localhost');
Can socket.io client connect to two different ports on the same
server?
I assume both machines share same domain. I believe it can use long-polling(websockets, flashsockets, etc also work), even passing along cookie. But I still need to test this on Internet Explorer because that browser never does what I want...
Can socket.io client connect to two different server?
The big question is if those both machines are on different domains. If on same domain it will work just fine even passing along cookie(s). If they are on different domains then on some browser we fall-back to json-p transport(worst transport imaginable), but it will work. Unfortunately then the cookie is not passed along, because of same origin policy. Right now I am toying to get around this cookie restriction(hard problem)...