Connecting a terminal based node.js app to a socket.io server? - javascript

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(){});

Related

AWS - How to connect to a specific port using Socket.io?

I have multiple Node.js servers running on an Amazon Web Service application. One of the servers is being ran on port 8080. How do I connect to this port?
Using var socket = io.connect('example.region-1.elasticbeanstalk.com') connects me to the primary server. What I need is something like var socket = io.connect('example.region-1.elasticbeanstalk.com:8080').

"Where" are Websocket servers hosted on Heroku?

I'm trying to host a Websocket server on my heroku app. There already is a GraphQL server running, but I don't think it's the cause of my problem.
So my server is started like this
const wss = new ws.Server({ port: port }, () => console.log(`Serveur WebSocket prĂȘt ${port}`));
There is no error, but when I try to connect to the server in my browser, just like this :
const ws = new WebSocket('wss://ethyme-api.herokuapp.com/');
I get an error 404.
So my question is, what is the path of the ws server, so I can connect to it ?
Thanks
If your Heroku app is called ethyme-api and and your locally run application is available under ws://localhost:$PORT/ the websocket will be available under wss://ethyme-api.herokuapp.com/ and ws://ethyme-api.herokuapp.com/

Standalone socket.io and Websocket

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

Socket.io-client simple client/server test wont connect - Node.js

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');

server-side connection to a socket.io server

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
});

Categories

Resources