socket.io-client v.09 to v1.0 connection issues - node.js

0 version of socket.io and I cannot find reference to the connect() function for the client. The issues I am having is that before I could connect like:
var socket = io.connect(
'localhost',
{
port: 3000,
reconect: true,
'force new connection': true
}
Now this does not work. My server is not getting the 'connect' event. Looking at the io after trying to connect like so noticed that the port number was not on the managers list and the uri was set to https://localhost. If I do io.connect('http://localhost:3000') works. Why is this? Does anyone know of any changes made to the connect that was not listed in the migrating section of the documentation?

Had a look at the sources of 0.9 and 1.0. I can't find anything called opts.reconect (typo?). All I could see was opts.reconnection. Also, I'm not sure what your 'force new connection' option is doing.
Anyway, I tested connecting via var socket = io();, as well as var socket = io.connect('localhost', {port:3000});. Both worked as intended.

Related

Websocket help using NodeJS on Ionos VPS

Im a noob here and in the field of what I'm trying to do too. I have started to create a multiplayer game but it was using short polling and I don't think that is going to be best long term.
After completing a basic POC I started to rebuild and decided to use websockets as a solution.
I'm running a virtual private Linux server from Ionos
Plesk is installed
I have created a subdirectory called server in httpdocs which is where the app.js file is for server creation.
Port 8080 is open
Im using http/ws for now before I attempt to go near https/wss
I have enabled NodeJS on the server and set up the following as config:
Document Root: /httpdocs/server
Application URL: http://[my.url]
Application Root /httpdocs/server
Application Startup File app.js
The app.js contains:
var Msg = '';
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('Received from client: %s', message);
ws.send('Server received from client: ' + message);
});
});
On any web page that loads on that domain I now get an error stating 'Upgrade Required' error 426
I know that means the server wants the client to upgrade to websockets but I cant understand why the client isn't asking to upgrade back.. In my ws_test.html file I access to test the connection this is the code in the HTML body:
<script>
let socket = new WebSocket("ws://[my.url]/server/app.js");
socket.onopen = function(e) {
alert("[open] Connection established");
alert("Sending to server");
socket.send("TEST SUCCESSFUL");
};
</script>
I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.
I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.

modbus-serial, is there a way to open and close the MobusTCP Server?

I am using the node package modbus-serial to create a TCP server. I can set up a server like so:
var serverExample = new ModbusRTU.ServerTCP(tcpip_parameters, {host: 'x.x.x.x', port: 123, debug: true, unitID: 1});
I couldn't find anything about opening and closing a server that has already been created. I was wondering if there is a way to do so like:
serverExample.open();
However, that throws and error that open is not a function. Is there a way to accomplish this?
When you pass a connection to the server instance it starts listening immediately.
You can see this issue.

Connect AWS redis to node using node-redis

I am using node-redis and having a hard time connecting to external redis instance. I tried with redis-cli and it worked. However with node I am not able to figure out how to properly give the url and port.
With Redis-cli-
redis-cli -h mydomain.something.something.cache.amazonaws.com -p 6379
However with nodejs
Below didn't work
var client = redis.createClient('redis://mydomain.something.something.cache.amazonaws.com:6379'),
neither
var client = redis.createClient({host:'redis://mydomain.something.something.cache.amazonaws.com', port: 6379});
How do I configure it. Please help.
Following should work with node.js -
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
Also, make sure that your security group on AWS allows you to access the database.
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
With the above code, it was always trying to connect with localhost,
Below code worked for me.
var client = require('redis').createClient(
{
url: `redis://${elasticCacheConnectionString}`,
}
);
Please note, i have appended redis:// as communication protocol before actual connection string.
FYI: I am using redis#4.1.0 version.

How to configure mqtt.js to connect to iot.eclipse.org over websockets

Edit: My issue is now this. I can connect to iot.eclipse.org using http://www.hivemq.com/demos/websocket-client, using port 80. When I connect via a browsified mqtt.js client I am getting the following error :
WebSocket connection to 'ws://iot.eclipse.org/' failed: Error during
WebSocket handshake: Unexpected response code: 200
I've tried ports 8080, 8000, 1883 and 80, without any luck. Any suggestions?
------------ Original question below -----------
I want to connect with a mqtt broker using mqtt over websockets. My client will need to run in a browser.
TO achieve this I am using mqtt.js library and am following these instructions.
Everything works when running against the public broker at broker.mqttdashboard.com. However when I connect to the public brokers at iot.eclipse.org and test.mosquitto.org I get HTTP errors.
I think the problem is incorrect configuration of the client when running against the second two brokers, but I'm struggling to find any help.
Heres the configuration, is there anyone out there who can help me?
// Works fine
var options = {
host: "broker.mqttdashboard.com",
port: 8000
};
// Doesn't work
/*var options = {
host: "m2m.eclipse.org",
protocolId: 'MQIsdp',
protocolVersion: 3
};*/
// Doesn't work
/*var options = {
host: "test.mosquitto.org",
protocolId: 'mosqOtti',
protocolVersion: 3
};*/
var client = mqtt.connect(options);
Let me know if theres any more information you need!
Mark
Both test.mosquitto.org and iot.eclipse.org are both websockets enabled (for a long time now actually).
You already have got test.mosquitto.org working - the key there is using port 8080.
The current iot.eclipse.org configuration expects the connection url to be ws://iot.eclipse.org/mqtt.
I don't think m2m.eclipse.org / iot.eclipse.org or test.mosquitto.org have websockets enabled.
broker.mqttdashboard.com runs a HiveMQ underneath which has native websockets enabled.
So in short, I don't think this is a configuration problem on your side. To make sure, you can check this web application and see if the other brokers work with that: http://www.hivemq.com/demos/websocket-client/

Socket.io, Redis Store and IE

I got a game using Redis, Socket.io, theres 2 nodejs servers running diff socket.io clients. I am communicating with both socket.io clients through the redis store, that way I can emit to all sockets whenever I want. And it works.
io.sockets.emit('successful_connection', { success : true }); return;
My problem is, when ie calling a specific socket by id, it fails.
io.sockets.socket(socketId).emit('successful_connection', { success : true }); return;
I have no idea why, it works in all other browsers. Heres the code for socket.io/redis store config
io.configure(function(){
var RedisStore = require('socket.io').RedisStore,
opts = {host: **.***.**.**, port: ****};
io.set('store', new RedisStore({redisPub:opts, redisSub:opts, redisClient:opts}));
});
Any advice would be helpful, right now my main thought is "why have nodejs/socket.io servers." Is there really a benefit if i have to deal with this. Thanks
This blog post with example code might help you.

Resources