Jwebsocket : How to send the request to a specific client by name - javascript

I am trying to establish a connection between a single server and lots of clients(more than 100). I am using websocket for the client and jwebsocket for the server.
The clients should always stay connected and the server should send request to a specific client inlcuding the name of the client.
It works to send the request but I am not able to send the request to a specific client.
Please help.

You are attempting to reinvent a 40-year old application protocol. You probably need either a chat or pub/sub (or similar) protocol that sits "over" WebSocket. Kaazing, ActiveMQ, NodeJS, et al, have these protocols already implemented over WebSocket. WebSocket is a transport; it is not a messaging system.

Related

Sending JSON to all online clients without them making a request. [NodeJS]

I'm building a website, and one of the features is a public chat that anyone online can use. When a message is entered it is sent to the server and then saved to a SQL database. How could I relay this information to all the online clients without them making a request to the server? I've thought about having all clients make a request to the server every 500ms or so but I feel that would be incredibly inefficient. Any suggestions?
What you're looking for is typically called "server push" where the server can unilaterally send data to the client without the client having to "poll" or repeatedly ask for new info.
The two general technologies for server push these days are webSockets and server-sent events (SSE). In both cases, the client initiates a connection to the server and that connection is held open so that the server can send data to the client whenever it wants to without the client having to specifically poll for that data.
A webSocket is a full, two-way data channel. Either client or server can send data in either direction.
SSE is a one-way channel, the server can send data to a listening client.
You can see these articles on comparing the pros/cons of each.
WebSockets vs Server-Sent Events - ably.com
Server-sent events vs. WebSockets - logrocket.com
Difference between server sent events and Websockets in HTML5 - geeksforgeeks.org
And, there are dozens of other articles here.
You may also want to be aware of socket.io which is a widely used layer built on top of webSockets that adds more features than either of these have (a named message layer, auto-reconnect, message acknowledgement, direct message response, built-in JSON support, etc...).
Any of these can do what you're asking for. Which of these to choose really depends upon the details of your requirements.
Try making a WebSocket server. There is ws package for Node.js, and alternatively socket.io. However if your client is a web client, you can use socket.io for easy-use and setup.

Interfacing data between functions in Node

I have a device that is connected to a Node server via WebSockets. To be able to query the device, a POST request to that server sends the necessary command via WebSockets. However, I'm not entirely sure what the best method of implenting getting the return is.
The flow is essentially:
Device connects to Node server via WebSockets
User sends POST request to endpoint on the same Node server
Node server sends the suitable command to the device via WebSockets
Device replies via WebSockets
This is where I'm stuck. How do I get the reply from WebSockets back to the POST request?
My first thoughts were as such:
The POST request knows what command has been sent
The device sends back the command as part of the WebSocket reply
Therefore, I can just have a simple array of messages from the device that the POST request function can poll, and if there's suitable reply (say within 2 seconds), reply with that data and delete the message from the array. Further down the line, I can delete any messages that have expired but are still in the array.
What are my other options? Presumably I could poll from the client doing the POST request or alternatively connect that client via a WebSocket.
This server supports 1 device and usually 1 user at a time, so there isn't really any worry that the server could become overwhelmed.

node.js (with socket.io) security?

since I am learning node.js I was wondering about something:
When I use node.js server to run a websocket, so that clients can connect (like on a website via javascript), it always listens public. Isn't that a security problem, that everyone in the world would be able to send data to the ip:port. They just have to connect to the server via the data that are written anyway within javascript and send / receive data?
I was thinking about a token, which would make sense in Java or Swift etc, but in javascript it can be seen anyway?!
TL;DR: yes, It's totally secure.
Every time the browser sends an HTTP request, there is a port waiting for the server's response. The difference between this to an open port is that an open port is open for everyone on the web. In an HTTP request or web socket, the port opens only to the server.

Maintain a backend connection on NodeJS across page requests?

I'm making connections via thrift (node-thrift) to a backend server to make api calls, but the communication is bidirectional (push/pull) to NodeJS.
While a user is browsing around different URLs, and Node is churning out jade templates and javascript files via Connect/Express routes, how do I maintain the connection to the backend server, and output (as an example) the connection status as part of the rendered jade output?
I have the connection object, but what do I do with it?
Sockets and port communication is generally a new area for me, so any help would be appreciated.
Keep in mind that backend server is not communicating to the web browser as the client, but rather the NodeJS server as the client.
(updated after discussion in comments)
So it looks like thrift is TCP-based which means the node client is going to keep the connection to your thrift API server open. This is entirely independent of what your node/express app server is doing with the browser clients. So if you keep a reference to your thrift client available to all requests, by attaching it to the app object for example, you should be able to determine it's current status and include that information in HTTP responses to the browser. There's not going to be any automatic coordination or association of any kind between your express app server handling browser HTTP requests and your thrift client making RPC calls to the API server. If you want coordination, you need to code that explicitly, but sending an HTTP response to a browser isn't going to automatically close your thrift TCP connection to the thrift RPC server (and same story vice versa).

Server WebSockets and server http with node.js

All
I am developing an application which needs to communicate with WebSockets with customers online in the browser. For this I started with Node.js to create the server, so far so good, my problem is that there is another application that must communicate via http POST to my server then passes this information to my server my server passes to clients web.
I am new to the use of this technology so I apologize if I say nonsense.
1 - I can send a POST request to my server WebSocket, the application that connects customers via WebSocket, Server, may also receive the POST request and send this information to each client by ws?
2 - There is a way to open a client socket with a tag that allows me, server, send information to that client only and not at all, depending on a parameter. I want to know that each socket belongs to a specific customer and only send information to that client.
1.) If I understand you correctly then you want to broadcast information through websockets to other clients. Just take a look at the node.js modules socket.io or ws.
2.) You want to open different sockets for different urls? Like a separate websocket connection for /foo/a/bar and /foo/b/bar? Have a look at my answer here

Categories