How to create a nodejs websocket client - javascript

I'm working on a project where I need to have multiple node clients connect to a node server via websockets. The clients will send files to the server and the server will immediately distribute that file to all connected clients.
The problem I'm running into is connecting as a client in node. The built in ws module seems to only support server use. I've also tried the npm websocket client which allows me to use node as a client but I seem to only be able to send binary data without any other information like the filename, etc. using the sendBytes method.
Thanks for any suggestions.

Checkout the delivery package of npm.
It provides bi-directional file tranfer for node.js over socket.io.
https://www.npmjs.com/package/delivery

Related

How to connet to the mysql server in zeit-now

I use zeit-now to deploy my website.
But I can't connect to my MySQL server.
I'm sure my MySQL server is able to be connected on my machine.
Here is my repo.
Any solution?
In the FAQ it says
Now itself focuses on being the best platform for serverless hosting. This means that the workloads we handle are static or stateless.
Still, you're trying to connect to your database on localhost. Although running a database instance in a docker container might be an option for a staging system, it definitely is not for production systems, since you will lose the data when you restart/redeploy. And you don't seem to startup a docker container in your deployment.
You should use a cloud database provider instead:
Use Cloud Databases that offer secure (preferably HTTPS-based) gateways, for example: Azure CosmosDB, MongoDB, Atlas, Firebase, Google Cloud SQL

How to create a react-redux app with node server as backend?

I am trying to create a react-redux app using node server as backend. Is it possible to make the node server serve the react-redux app instead of running react-redux using dev server a=in one port and node on another port?
Need some idea to start with. Thanks in advance:)
Yes.. it is
you can serve you react app on '/'
and listen for API request in another route
so you don't have separate codebase for the react app and the api backend code
You can use express to serve the react app on a particular route
i.e my-app.com/
then serve backend related content on another route
i.e
my-app.com/api
so when a request is made to my-app.com/ express serves express serves backend resource or API
There are a few steps I take when creating an express/react app together. I'll create a server and a client directory. The client dir is created with create-react-app and the server can be created via express-generator for example. My project dir (the one that contains both of them) is basically just glue that melds the two together. In the client app, I'll add proxy:localhost:3001 (or whatever port your express api is running) and I use concurrently to run both servers (client and server - as client is being run by webpack-dev-server) at the same time. They run as separate servers during development, but when I make an api call, it's as if I'm making it directly to the express server itself.
The only other thing to worry about is deploying the application. You can use the build command that comes with create-react-app and copy that over to the public directory in your express application that is served up via express.static.
Here's a quick example to take a look at:
https://github.com/overthemike/heroku-skeleton
This is an useful doc for redux SSR setup. This helps avoiding running client and server at two ports.
Redux SSR

WebSocket client can't connect to cloud server through corporate proxy except for web browsers! (ETIMEDOUT)

I'm trying to connect a WebSocket client (corporate Node.JS server) to a Cloud server, but it times out (connect ETIMEDOUT error).
I'm not sure which needs configuration... Linux, Node.JS or the WebSocket Client?
I've already configured the proxy for Linux (export proxy/https_proxy), and for Node.JS (npm config set proxy/https_proxy) but the problem persists!
I'm using the ws library found in https://npmjs.com and implemented it without any sort of options except setting port.
Any advice?
EDIT: Works fine in the web browser, by the way.
node does not automatically use any form of configuration for making HTTP requests through proxies -- ie, node does not read the PROXY or HTTPS_PROXY environment variables. npm config set proxy only affects npm itself.
In other words, node programs always try to access servers directly. It appears your network requires HTTP requests to go through a proxy, and direct connections are being silently dropped.
If you want your program to make HTTP requests through proxies, you must do so manually. The http-proxy-agent module can help you do this.

Enable to get socket.io file on client

I using a angulajs as frontend and node.js REST API as backend both our on different server. I want to use socket.io, but enable to get socket.io.js file on client from the node.js server i am using
<script src="http://localhost:3000/node_modules/socket.io/socket.js"></script>
to get the socket.io file.
Error:
GET "localhost:3000/node_modules/socket.io/socket.js "
I highly recommend taking a look at this library:
https://github.com/btford/angular-socket-io
This way you can easily integrate socket.io with Angular for client side

Is it possible to set up a socket.io client running (server-side) on a node.js server?

I'd like to enable socket-based p2p communications between two or more different node.js application servers. I'm using socket.io to handle all such communication between a given server and the web application it serves - but what I'm looking for is a way to communicate server-to-server.
I had initially assumed it would be as easy as something like this:
var io = require("socket.io");
var socket = io.connect("my remote endpoint");
However, as it turns out the server-side socket.io implementation doesn't offer a "connect" method, only a listen method.
Why is this? Why can't I treat a node application server as a client to a socket.io server running elsewhere? Is there any way that I can achieve this functionality?
OK, so thanks to #pimvdb in the comments above I've got a workable solution.
Basically, the socket.io library that npm installs has a dependency on another module, called socket.io-client. In a standard socket.io installation this will be installed in node_modules/socket.io/node_modules/socket.io-client
However, it's also possible to say "npm install socket.io-client" and install it as its own first-class citizen library.
Then your usage looks like this:
var client = require("socket.io-client");
var socket = client.connect("http://myendpoint.com:3000/whatever");
socket.emit("test", "foo");
And everything works.
So, thanks man!
Just for clarification, this is an example with listeners and possibility to emit events (and without install again a module already installed)
var io = require('socket.io/node_modules/socket.io-client');
client = io.connect('http://'+CONFIG.host+':'+CONFIG.port);
client.on('connect',function() {
client.emit("test","foo");
});
Before you go full speed on socket.io for server-to-server communications.....
socket.io is engineered as a browser to server comm infrastructure. I'm far from certain it is the best solution for P2P server stuff. Plus, if you do server-to-server - why not just do Websockets? There are various websocket modules for node - e.g. https://github.com/einaros/ws

Categories

Resources