I have a Java server that connects with web-browsers through web-sockets, specifically socket.io, specifically this implementation: https://github.com/mrniko/netty-socketio
Now I would like to connect to the same server, not from a web-browser (through Javascript), but from a Node.JS program.
The problem is, in all examples that I found, the Node.JS program is the server, and the clients are always web-browsers.
I tried to use a code similar to the client connection code in node.js:
var io = require('socket.io');
var socket = io.connect("http://localhost:9080");
but got this error:
TypeError: Object #<Object> has no method 'connect'
You need to use socketio-client
$ npm install socket.io-client
https://github.com/LearnBoost/socket.io-client
You can then connect to another socket server by the following:
var io = require('socket.io-client');
var socket = io.connect('http://domain.com');
socket.on('connect', function () {
// socket connected
});
Related
I have a Websocket program running on Ubuntu machine on Digital Ocean. The code is shortened below.
// server.ts
import ws from "ws";
const wss = new ws.Server({
port: 3002,
});
const handler = applyWSSHandler({ wss, router: appRouter, createContext });
console.log("WebSocket Server listening on ws://localhost:3002");
...
I host the server using nodemon server.ts or pm2 start server.ts. The program compiles and logs that Websocket Server is listening.
Now, here is a minimum code for a client which just checks if Websockets is active.
// client.js
const ws = new WebSocket('ws://xxx.xxx.xxx.xxx:3002'); // where xxx.xxx.xxx.xxx is the IPv4 of my Ubuntu machine
ws.onopen = function (event) {
console.log('connected');
};
Nothing gets logged, showing that the Websocket server isn't active.
I expect "connected" to be printed.
If I host my server locally instead of on Digital Ocean, and connect to "localhost:3002", "connected" does get printed out.
Is there something wrong with the way I am hosting the server, or am I connecting to the server wrongly?
Any help is appreciated. Thanks!
I have a Python file which talks to a JavaScript file via socket.io and socket.io-client. The debugger output on the server side is shown below.
Debugger output: https://i.stack.imgur.com/pL1p7.png
I've tried implementing some fixes recommended in similar posts, for example I changed
const socket = io(SOCKETIO_URL)
to
var options = {};
options.transports = ['websocket'];
const socket = io(SOCKETIO_URL, options , {transports: ['websocket', 'polling', 'flashsocket']});
but that just yields this error: https://i.stack.imgur.com/QcUEX.png.
Any help would be appreciated.
I was running into a similar issue and turned out it was caused by version mismatch between socket-io client and socket-io server packages. So make sure you're using similar versions socket io on your server and client.
I am able to connect a web based app to a socket.io server, but not a terminal based app, this is what i use to connect to the socket.io server:
var socket = require('socket.io')
var connection = socket.connect('http://127.0.0.1:8080');
I get the following error:
TypeError: socket.connect is not a function
How can I connect a terminal app to a socket.io server, written in node.js
The socket.io is for creating WebSocket server. You can't use this module as a client. Instead try socket.io-client
var socket = require('socket.io-client')('http://127.0.0.1:8080');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
I need to build a socket.io server that will intercept incoming connections from an app which is not stored in the same directory as the server.
The client side app does not contain node.js, thus I'm trying to use a websocket :
Telnet.Socket = new WebSocket('ws://127.0.0.1:3000');
My node.js server does not need a http server but must be a standalone socket.io app. Thus, I've tried the following code :
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('connexion entrante');
});
io.listen(3000);
Unfortunately, the server part does not seem to get the Websocket connection request. My firefox says :
Firefox cannot establish a connection with the server at adress ws://127.0.0.1:3000/.
What am I missing ?
Thx in advance !
socket.io need client use socket.io to connect because it use many kind of connection. For connect websocket only you can use ws node module
I am trying to get some simple communication to work in Node.js using socket.io and socket.io-client.
I have two scripts, server.js and client.js.
The server.js script can bind to a webfacing port and act like a normal server/socket.io host, which all works when used with the browser client,
but the client.js (running in another node script) doesn't work, it just waits not outputting anything. I expect my socket.io-client in client.js to connect to the socket.io instance in server.js and for both to display a message in their console to say they're connected.
server.js code:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
console.log("\nNew server request: "+req.url+"\n");
// sends a html file with a script that connects
// to socket.io running in server.js
res.sendFile(__dirname+'/webroot/index.html');
});
// listens for connections
io.on('connect', function(socket){
console.log('a client connected');
});
// start listening on server
http.listen(9000, function(){
console.log('listening: 9000');
});
client.js code:
var io = require('socket.io-client');
// connect to server.js socket
var socket = io('http://hostsite.com', {
port: 9000
});
socket.on('connect', function(){
console.log("connected\n");
});
installed with npm
npm install socket.io // version: 1.0.6
npm install socket.io-client // version: 1.0.6
npm install express // version: 4.8.5
Ideally I don't want to be using express or an http server, just a socket communication between two Node.js scripts, one on a server machine, one on a client machine.
Thanks for any help :)
For some reason, it wouldn't connect when i used:
npm install socket.io-client
and
require('socket.io-client');
But it does connect if I use the client that comes with the main package
npm install socket.io
and
require('socket.io/node_modules/socket.io-client');
Took a while to figure out, but hopefully now it will take someone else less time; if they face the same problem.
have you tried:
var socket = io.connect('http://hostsite.com:9000');