Right now I am setting the nodejs server to use webscoket to receive the data from other server
I have tried to use websocket(ws) in nodejs server, and it can connect to the other server
const WebSocket = require('ws');
var ws = new WebSocketClient();
ws.open("ws://ws.something/?token="+Token);
But when I try to connect using socket.io, the console does not display a debug message, and no data is received
var io = require('socket.io-client')
const socket = io.connect('ws://ws.something/?token='+Token);
socket.on("connection", function(mSocket){
console.log('debug message')
mSocket.on("message", function(myData){
console.log(myData)
});
});
socket.on('error', function (err) {
console.log(err);
});
Can socket.io connect to ws:// ? Or is there something wrong with my code? (This is the first time to use it)
No, it cannot.
Socket.IO is a layer on top of several transports, with Web Sockets being only one of them. Socket.IO clients can only connect to Socket.IO servers.
If you want to connect to your Web Socket server from a browser, use the browser's built-in Web Socket client:
const socket = new WebSocket('wss://example.com');
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
Related
I have created a server using net module:
// Creating and connecting with server
const net = require('net');
const server = net.createServer(); //Creating server
//Connecting with server
server.on('connection', function (socket) {
let remoteAddress = `${socket.remoteAddress},${socket.remotePort}`
console.log(remoteAddress)
console.log(`connection is established... ${Date.now()} \n `);
socket.write(`connection is established...${Date.now()} \n`);
//Receiving and Sending payload from/to client
socket.on('data', async function (payload) {
console.log("payload from client",payload)
socket.write(`acknowledge : ${payload}`);
});
//Close connection
socket.on('close', function () {
console.log('Server Connection Closed');
});
//Server error
socket.on('error', function (err) {
console.log("Caught flash policy server socket error: ")
console.log(err.stack)
});
});
server.listen(8001, function () {
console.log('Server Listing on Port 8001');
})
I deploy this code to AWS EC2 and When I tried to connect with telnet client (telnet ec2_ip 8001), initailly it is working but after sometime it is giving following errors.
screenshot of telnet client:
screenshot of ec2 logs:
And When I tried to connect with real IOT scooter with ec2 Ip address and port 8001, It is not connecting for even a second.
Can anyone please tell me what I am doing wrong?
Note: I don't have much knowledge about IoT. This is the first time I am connecting an IoT scooter with nodejs.
It is hard to know exactly what your problem is without seeing more of your code, or getting more explained. Like what happens in your real code? The logs seem to indicate that you are making outbound calls to another service after the initial connection. If that is the case, are you handling error conditions of your dependencies correctly? An unhandled exception would kill your server, and it would need to be restarted to work again. If you are using dependencies that fail, for instance databases, other servers, configuration files etc that fail on your scooter, that may be why it fails without even allowing one single connection. But without knowing more specifics about your application and architecture, it is just wild guesses at this point.
Your server example code, seems fine. As long as you have opened for traffic on that port in your routing configuration in AWS, it should respond. It could be that you just simply run out of connections, but again that requires me to know more to give you a definitive answer.
My advice would be to check your dependencies, and your logs for supporting systems.
I've got a socket.io server running on Node JS listening for messages. I now need to start another Node JS script (my client) which will send messages to the server, that's at a specific URL and port.
I'm passing my URL into the connect function but am getting the error:
TypeError: io.connect is not a function
How can I pass my URL to connect to into this. My script is:
const io = require('socket.io');
const socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
socket.send('hello') // send to server from running JS script
});
If you're attempting to initiate a socket.io connection from node.js, you need the client-side library which would be:
const io = require('socket.io-client')
And, of course, you have to install that library.
I created my Socket server with Express, SocketIO and Redis
server.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8890, function (e) {
console.log(e);
});
io.on('connection', function (socket) {
console.log("new client connected");
var redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on("message", function(channel, message) {
console.log("mew message in queue "+ message + "channel");
socket.emit(channel, message);
});
socket.on('disconnect', function() {
redisClient.quit();
});
socket.on('connect_error', function() {
redisClient.quit();
});
});
From command line, I run node server.js. Its worked.
I also created a html file to connect to that server.
From Browser Console, I run io.connect('http://localhost:8890'). I got as the result
As I see, too much connections (requests).
What happens? What wrong from my code?
You have mismatched client and server versions causing the initial connection to fail and the older client is dumb enough to just keep trying over and over again. If you are using 2.0.4 on the server, then you must use that version for the client too. If you serve the client version directly from your server with:
<script src="http://localhost:8890/socket.io/socket.io.js"></script>
Then, the socket.io server will automatically give you the right client version.
Or, you can manually link to the right version on your CDN such as https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js. But client and server versions MUST match.
The advantage of getting the client directly from your own server is that anytime you update your server version of socket.io, the client version will automatically be upgraded for you and kept in perfect sync since the matching client version is built into the server version.
I have 2 Raspberry Pi running on the same Network.
I am using one as a local web server for my house, I then have another one connected to some devices. I want them to both be able to communicate to each other via web sockets but am having some problems.
My server looks like this:
express = require('express'); //web server
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server); //web socket server
server.listen(8080); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', function (socket) { //gets called whenever a client connects
socket.on('Testing',function(data){
console.log("Testing connection");
});
});
My problem comes with the client connection I am really not sure what code to use to try and connect.
I have installed Express and Socket.io on my client and used this code:
console.log('1');
// Connect to server
var io = require('socket.io')
var socket = io.connect('http://192.168.1.138:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
But this leads to an error on the io.connect is not a function.
I am not really sure how to get the client to work so any advice is appreciated.
I should add that connecting to my webserver directly via the ip and port does load the webpages I have created successfully.
When using socket.io on the server side as a client you need to do var socket = require('socket.io-client')('http://192.168.1.138:8080', ...);. See https://www.npmjs.com/package/socket.io-client
I am in the process of moving a socket.io/node.js server from my local machine to a digitalocean vpn, but I am experiencing some strange connection issues.
I don't see any errors thrown when I try to connect, but the 'connection' event isn't fired on client or server side.
However, if I close the server, I get this error on client side: GET http://myip:8000/socket.io/1/?t=1410475126223 net::ERR_CONNECTION_RESET
So it seems that it is actually connecting, just not firing any events?
Here is my server side code:
var app = express();
var server = app.listen(8000);
io = require("socket.io").listen(server);
io.sockets.on("connection", onSocketConnection);
and client side:
socket = io.connect("104.131.41.120", {port: 8000});
socket.on("connect", onSocketConnection);