The following code only works in the browser. It does not work in node.js
let socket1 = io('http://localhost:3031/nsp')
socket1.on("connect", (error) => {
console.log("socket1: connect")
});
I am connecting to a netty-socketio (v1.7.7) java server. I am able to connect to the root namespace on both browser and node.js clients and everything works as expected. However, if I try to connect to a namespace, only the browser client can connect as expected.
Furthermore, I can see on the server that the node.js client (v2.4.0) is connecting to the root namespace when it should be connecting to the named namespace "nsp". As such, the clients join the root namespace and seemingly never join the "nsp" namespace.
On further inspection, the only event I can get to fire on the node.js client, when specifying a namespace, after connecting is "ping" all other events (connect,connect_error,error,reconnect...) never trigger.
Update: the above code works when connecting to a node.js server, so the issue appears to be with the netty-socketio server.
Here is how the netty server is initialized:
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(3031);
server = new SocketIOServer(config);
server.addNamespace('/nsp')
I finally figured it out. I had to revert the node.js client version to 1.7.4 (https://www.npmjs.com/package/socket.io-client/v/1.7.4) to get it to work. Not sure why I was forced to use such an old client to be able to use namespaces.
Related
I have a 3rd party server that runs SignalR. In order to connect to that server and read messages i have created a javascript code that should run from NodeJS on ec2 (Linux) instance. The issue is that when i try running my code that only connects to the server i'm getting this error:
.....node_modules/signalr/jquery.signalR.js:1085
}(window.jQuery, window));
^
ReferenceError: window is not defined
What kind of window? I have an ssh-only linux server? My existing JS code is this:
const signalR = require("signalr"); //here i can't use #aspnet/signalr or #microsoft/signalr because those aren't compatible with SignalR that's on the 3rd party server. UGH Microsoft!
let connection = new signalR.HubConnectionBuilder().withUrl("https://myurl/signalr/hubName")
.configureLogging("warn")
.build();
connection.start().then(() => {
console.log("yeeey!");
}).catch((e) => {
console.log(e);
});
What are my options here in order to run this? I have installed NodeJS on ec2 instance using official Amazon tutorial so it should be the latest current version - 13.2
Found a solution.... This library solved my problems
Please write this:
import * as signalR from '#aspnet/signalr';
I am using sockets.io with nodejs server to send/receive events between client and server.It works well on local pc but the issue comes when i have to connect a remote client to it and i dont need to use frontend template file or html. In that case, how can i define io() object i.e. a socket connection because without it i cannot access my nodeserver. Currently i have created a js file on remote client and it looks like this:
`var socket=io.connect('http://a.b.c.d:3000');
var name="alice";
socket.emit('ping',(name)=>{
socket.on('pong', reply);
console.log(reply);
});`
Web Sockets work over the HTTP protocol, so you need to run a server for it.
I have a problem with socket.io#^1.0. The setup is fine because it works locally, the server is correctly configured and when i try to connect to the server from my Angular APP it works fine with this:
io.connect("localhost:8080");
The connection is established and i can send and receive event. Now in the production environment, "locahost:8080" is replaced with the address of the server Launched:
io.connect("https://domain-name.com/api");
I know that the problem here is the /api, since socket.io is considering it as a namespace and it's trying to connect to it, in my network console I see 500 Internal server error with the address https://domain-name.com without the /api when i replace the request url to add the /api I get a 200 OK with type octet-stream.
So the question here is: how do I connect to the correct path without consideration of the namespace?
Thanks in advance for any help :)
I think you want to use the path option (documented here):
// client
var socket = io.connect('https://domain-name.com/', {
path : '/api/socket.io'
});
OK this is very simple to anyone who's used websocket and nodejs.
I have created a websocket server named ws_server.js and put it in C:\Program Files (x86)\nodejs where I have installed the nodejs framework. I started the server and it is running and it says it's listening on port 8080. So far so good, I have the server running.
Now I simply want to connect to it from client code so that I can do all that lovely stuff about capturing events using event listeners etc. The problem is, embarassingly, I cannot figure out what URL to use to connect to my websocket server.
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://localhost:8080/"); // WHAT URL SHOULD BE USED HERE?
websocket.onopen = function(evt) { alert("OPEN") };
websocket.onclose = function(evt) { alert("CLOSE") };
websocket.onmessage = function(evt) { alert("MESSAGE") };
websocket.onerror = function(evt) { alert("ERROR") };
}
function doSend(message) {
// this would be called by user pressing a button somewhere
websocket.send(message);
alert("SENT");
}
window.addEventListener("load", init, false);
When I use ws://localhost:8080 the only events that trigger are CLOSE and ERROR. I cannot get the client to connect. I must be missing something very simple. Do I need to set up my nodejs folder in IIS for example and then use that as the URL?
Just to reiterate, the websocket server is running fine, I just don't know what URL to use to connect to it from the client.
EDIT: The websocket server reports the following error.
Specified protocol was not requested by the client.
I think I have got it working by doing the following.
var websocket = new WebSocket("ws://localhost:8080/","echo-protocol");
The problem being that I needed to specify a protocol. At least now I get the onopen event. ...if nothing much else
I was seeing the same error, the entire web server goes down. Adding the protocol fixes it but leaves me wondering why it was implemented this way. I mean, one bad request should not bring down your server.
You definitely have to encase it a try/catch, but the example code provided here https://www.npmjs.com/package/websocket (2019-08-07) does not. This issue can be easily avoided.
I just wanted to share a crazy issue that I had. I was able to connect to a websocket of an old version of a 3rd party app in one computer, but not to a newer version of the app in another.
Moreever, even in new computer with the new version of the app, The app was able to connect to the websocket, but no matter what I did, when I tried to connect with my own code, I kept getting the error message that the websocket connection failed
Long story short, They changed an apache configuration that allowed connecting to the websocket via a proxy.
In the old version, apache config was:
ProxyPass /socket/ ws://localhost:33015/ retry=10
ProxyPass /socket ws://localhost:33015/ retry=10
In the new version, apache config was changed to:
ProxyPass /socket/ ws://localhost:33015/ retry=10
By bad luck, I was trying to connect to ws://localhost/socket and not to ws://localhost/socket/. As a result, proxy was not found, and connection returned an error.
Moral of the story: Make sure that you are trying to connect to a websocket url that exists.
For me, the solution was to change the URL from ws:// to wss://. This is because the server I was connecting to had updated its security, and now only accepted wss.
step:
start a derby app
write a standalone node client app , set up socket.io-client , connect to derby's store's sockets
questions:
the client is not run at browser, so it don't has session, can it pass Racer's socket authorization?
how to write dery service that can listen message from client, then put it into derby store/model?
You can fallback to ajax by having following in your /server/index.js
require('derby/node_modules/racer').io.set('transports', ['xhr-polling']);
Checkout example