I have found some node.js code for my Intel Edison that has the basis of what I need the issue is that it's using socket.io buttons on the html front end to control GIPO pins on the board but it does not read the current state of the GPIO pin when loading the page or if the page is loaded on another device they are not synchronized. I was thinking that booleans could be set on the node.js code that would hold the status of a GPIO and the webpage would constantly check the status of it and set the button state accordingly? I tried some stuff my self but as a beginner I was out of luck. The code in question is on gethub https://github.com/drejkim/LediMote
Given the socket.io-based app structure you already have, it looks like you should just read the initial GPIO state and send it to the web page with a socket.io message as soon as you get the connection event for the incoming socket.io connection in /lib/routes/socket.js.
Just create a message name and send that message with the initial state upon connection. Then in the client, you just listen for that message and update the state in the web page with whatever is sent as the current state. You can use that same message to update any connected clients any time the state is changed by some other client.
Related
I am looking for a solution to my problem. I have Node.js server serving my web application where user can log in. I want to handle a situation where one user A performs specific action and user B associated with this action gets real life notification. Is there a module that would help me or there is some other solution?
What you are describing is "server push" where the server proactively notifies a user on their site of some activity or event. In the web browser world these days, there are basically two underlying technology options:
webSocket (or some use socket.io, a more feature rich library built on top of webSocket)
server sent events (SSE).
For webSocket or socket.io, the basic idea is that the web page connects back to the server with a webSocket or socket.io connection. That connection stays live (unlike a typical http connection that would connect, send a request, receive a response, then close the connection). So, with that live connection, the server is free to send the client (which is the web page in a user's browser), notifications at any time. The Javascript in the web page then listens for incoming data on the connection and, based on what data it receives, then uses Javascript to update the currently displayed web page to show something to the user.
For server sent events, you open an event source on the client-side and that also creates a lasting connection to the server, but this connection is one-way only (the server can send events to the client) and it's completely built on HTTP. This is a newer technology than webSocket, but is more limited in purpose.
In both of these cases, the server has to keep track of which connection belongs to which user so when something interesting happens on the server, it can know which connection to notify of the event.
Another solution occasionally used is client-side polling. In this case, the web page just regularly sends an ajax call to the server asking if there are any new events. Anything new yet? Anything new yet? Anything new yet? While this is conceptually a bit simpler, it's typically far less efficient unless the polling intervals are spaced far apart, say 10 or 15 minutes which limits the timeliness of any notifications. This is because most polling requests (particularly when done rapidly) return no data and are just wasted cycles on your server.
If you want to notify userB, when both of you are simultaneously online during the action, then use websockets to pass message to a two-way channel to notify userB.
If you want to notify them whenever, regardless of online status, use a message queue.
I m working on Node js server.
In order to communicate with client i open websocket which should last for the whole user session. I need also to check if a user goes offiline. One way should to be to check the state of the web socket i.e. isReady or closed. I read there some situation which the onclose is not called. So what can be a trustable way to count the real current connected users?
Socket.IO timeout (disconnect) occurs if there are no activity present in the socket, but how is detected that are no activity? I dont find information about that. How works the detection process? For example:
If user closes the website tab, occurs disconnect?
There is any way user loses connection and disconnect will not be executed?
Is possible cache information informs that Im online even if I leave the website?
If you open your browser's developer tools in the network panel, you can filter your requests to ws requests (web socket requests), in there you can see your active web sockets connections. If you choose one connection, you can see the headers, the frames, the cookies and timing. If you choose the frames option you can see what's being sent and received, between your browser and your web socket server.
The next image will make it clear for you, it's chrome's developer tools:
Now in there you can see there are some numbers, basically your browser and your server are doing ping pong. You can read more about these numbers in this answer SocketIO Chrome Inspector Frames
This ping pong is what keeps the socket alive so we know that there are no timeouts. As for the disconnect and the connect I advise you to read more about the WebSockets API, in there you can see there are event listeners for onclose, onerror, onmessage and onopen.
So answering this question:
For example: If user closes the website tab, occurs disconnect? There
is any way user loses connection and disconnect will not be executed?
No, the onclose event will be fired, but even if any cosmic reason the onclose isn't fired you will eventually disconnect due to timeout.
As for your other question:
Is possible cache information informs that Im online even if I leave
the website?
Yes, that's not up to sockets, that's up to you and your implementation. You can keep a list of online users and only update that list from time to time, let's say 10 minutes. You can keep the online users lists and between the time you update your online users list, some of them are already disconnected.
will my server be notified about disconnect on the client side?
I assume the answer is yes if the disconnect happens explictly like below.
pusher.disconnect()
however what happens if the user simply closes the browser?
Another thing is there a way to notify the server that a certain channel has not been in use by the client(s) for some while?
The connection states documentation shows how to bind to connection state changes.
however what happens if the user simply closes the browser?
This really depends on if the browser calls webSocketInstance.onclose so the Pusher JavaScript library is informed before the browser is closed. You could always detect this yourself using window.onbeforeunload, window.onunload or the addEventListener versions.
Another thing is there a way to notify the server that a certain channel has not been in use by the client(s) for some while?
You can use WebHooks so that when a channel becomes vacated your app server will be informed.
I have problem detecting the loss of socket connection in CF app for PDA device.
I have static class that has static methods for communication (Connect(), Write(), Disconnect()). Static because all forms can call Write method.
In Connect method i call socket.Connect(ipEndpoint);
But when device hasn't got wifi connection program halts at this line for about 20 s which is too long. Also if user starts Write() method (saving some data) and wifi connection is lost, user cannot interact with form and thinks that application frizzed. Since there is no timeout option for CF socket connection, what is the best way to control socket behavior?
My idea is to show some kind of "Communication form" when socket doesn't response for 5 seconds which will try to reestablish connection. This form will have graphical indicator (rotating clock or something like that) to show user that program is trying to connect and exit button if user decides to exit app. If socket.connect succeeds, i will show last used form to user.
I assume that this has to be done with Threads, but since i don't have experience with it. i need help how to manage this behavior.
You can call Socket.BeginConnect() to launch the connect in the background. You can then specify the callback method that will get invoked when the socket has connected (or timed-out). Additionally, to implement your progress bar counting down as it tries to connect you can do:
IAsyncResult ar = moSocket.BeginConnect(...)
And then you can have your connection form use a timer to count down, checking the status of the connection by calling:
ar.IsComplete
Polling is not very efficient, but in this case it works well with your described pop-up connection form.