Interfacing data between functions in Node - javascript

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.

Related

can we listen to incoming messages in node.js?

I have a registration form that sends a 5 digit number to confirm mobile phone, and the site gets the digits in next form, I want to take it one step further and if the user is registering on his phone listen on messages and auto-fill the input.
so is it possible to do that? listening to incoming messages from a browser?
if we can, how?
thanks.
You can have 2way communication with the server using WebSockets. One of the easiest ways to use WebSocket is with https://socket.io/
You create socket server with nodejs, connect with socekt.io client from browser and server can push any event to the browser just like a browser can push any event back to the server.
WebSocket communication is considered as real time while it does not create new HTTP handshake stuff.
If you want to read user's SMS. You have to have permission to do so. You can't read SMS from web application running in browser. Only way to work around i can think is this pattern:
your user has your native application which can read sms and runs in a background
your app reads sms and sends notification to your websocket server about it
your browser receives confirmation from the same WebSocket about message received

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

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.

Node.js server, importing and exporting data to and from client

I am making a node.js server and I have the code to get the server running. However, I am not sure how to get data from the client into to database. This is a game I am working on which I want to make multiplayer so I am new to node.js. Every player has a different picture on their screen and I am using javascript to draw on a canvas in my html file. How do I get information from the player and then query that to then give them an output to draw on their screen.
Thank you the sooner this can be answered the better
A browser client can do one of three things with a server.
It can request a new web page thus changing the active page in the browser (probably not what you're asking for).
It can send an ajax call to the server and receive a response from the server. An ajax call can either be used just to send information to the server or it can be used to get information from the server and then display that information to the user by changing the currently displayed web page.
You can create a lasting webSocket connection to the server. After the webSocket connection is created, then the server can send the client new data or requests or the client can send the server data or requests. Data or requests can be sent either way.
If you just want to send from the web page to the server so that the server can store something in the database, then you would likely use the 2nd option (an Ajax call). You would create a route in your node.js server (e.g. a specific URL for this Ajax call) and then from your client, you would make an Ajax request to that specific URL. You can also send data or parameters to the server with the Ajax request.

Maintaining Connection using WebSockets

I have a JavaScript/html web socket client. It receives images from my server quite frequently. I am hosting my web socket server in a C# console application.
The longest my client has to wait for an image is 10 seconds.
Should I also be sending 'keep-alive' packets from my client (JavaScript) to my Server or is the fact that it receives regular data packets from my server enough?
Thanks
You can send ping messages from the server, and the client will answer with a pong message. It is part of the WebSocket protocol, not sure if your server implementation supports it. However, from the client you have no way to know if you got a ping message in while, so the client may be let hanging there.
Or you can do it at application level using your own messages. Basically create a "ping" message in JSON and send it regularly from the client to the server, then if you do not get a response in X time, reconnect.
If you donĀ“t, the only problem is that the connection maybe half-open, and your client may be hanging there waiting for a image that will never come.
Cheers.

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