Would socket.io receive data sent via websockets? - node.js

I have this IoT device that I am trying to connect to the server via wifi to send real time data. The server uses socket.io with node.js, but the wifi module (esp8266) I am using only has a websocket package. Would a socket.io connection be able to receive data sent via websocket?

Yes, but it will be a bit of a kludge:
socket.io supports multiple transports, of which one of them is a websocket. You can connect using only the websocket, but you will need to implement the socket.io protocol.
https://github.com/socketio/socket.io-protocol
If you are using a popular platform, there may be libraries available, i.e. for Arduino, there is:
https://github.com/billroy/socket.io-arduino-client

Related

Communication between REST and UDP server

I have a REST server to handle communication between my database server and Android/iOS devices, the REST server is also able to send push messages via Firebase. My second server is a UDP server, that receive and send messages to a IOT device, both server are written in Node.js and running on different EC2 instances.
Then my UDP server receive a message from the IOT device, lets say some GPS data. Is there a good way to call some methods from my REST server via the UDP server? Or send the data to it ? Are there any ways that the two server can communicate with each other ?
You could implement a separate API on your REST server that would be called from your UDP server.
Interprocess communication is a wide topic, there are plenty of ways to do it, it all depends on your needs.
via http
via tcp/ip or udp
via a database (or even a file)
using named sockets (on unix/linux)
using a pub-sub library
using a message queue library
by piping standard input/output

Can a socket.io server communicate with a non-socket.io client

I am building a chat server which uses a custom protocol for communicating with clients over sockets. The client sends specific strings to the server and the server must take an appropriate action based on these non-standard messages. I can't change this protocol, nor do I have any access to the underlying client code.
My question is, can I use the node.js socket.io package to power the server socket communication if I have no idea how the client is handling it's socket activity? I'm asking because, reading through the socket.io docs, each time I push anything through a socket an 'event' is associated with each action.
Is it possible to send a very exact message to the client from the server with this 'event' bundled in? Am I better off using the websockets package?
Can a socket.io server communicate with a non-socket.io client
No. A socket.io server requires both the webSocket protocol for connection initiation and the socket.io format on top of that. So, a socket.io server can only talk to a socket.io client and vice versa.
If your chat client uses a custom protocol, then you need to implement a TCP server that also speaks that custom protocol (whatever it is).
If you can modify the client, then you can modify it to use a socket.io client and then you can send your chat messages via socket.io where your socket.io server can then receive those messages.
The client sends specific strings to the server and the server must take an appropriate action based on these non-standard messages. I can't change this protocol, nor do I have any access to the underlying client code.
Then, you have to implement a server that speaks your custom client protocol based on whatever the underlying protocol is for the client. There is no other way around it.
I'm asking because, reading through the socket.io docs, each time I push anything through a socket an 'event' is associated with each action.
This is how the socket.io layer works. It sends a message and (optional) data. This can always be used to just send data by just declaring a generic data message and then just listening for the data message on the other end. But, this assumes you can modify both client and server to work this way. If you can't modify your client to use the socket.io protocol, then you can't use socket.io.

Connecting a web client to a c++ server with TCP

i wrote a simple C++ Server which is waiting for clients.
I want users to be able to connect to this client by opening a webpage without installing anything. So the only thing that comes to my mind is using Javascript. Since the server needs to react in realtime later on ( performance is an issue ) sending data with POST/GET is not wanted.
Is this possible with Node.js or Socket.io ? I am trying to find a good example but i only find node.js servers. And when i open a socket with
var socket = io.connect('localhost:25003');
it is sending weird data.
Does anyone have a simple example with a javascript client that can connect to anything and send raw data?
You cannot do plain TCP from browsers (unless you're writing a browser addon/extension or using something like a Java applet or a Flash bridge possibly). Socket.IO always uses HTTP. However those HTTP connections can then be upgraded to WebSockets or other protocols, depending on what the browser and/or Internet connection supports.
The "weird data" you're seeing is probably an HTTP message.

How to access TCP Socket via web client

I have a program in an embedded device that outputs an xml string to a socket. The embedded device has lighthttpd has a web server. I want to use a web based client (no flash/silverlight) to connect to the socket and pull the xml data every second.
I looked at Node.js with Socket.io to get what I want to do, but I am not clear about how to proceed. Searching through the Node.js and Socket.io documentation and examples I see standard client-server behavior, nothing regarding what I am trying to do.
Basically, the web server is just there to accept a connection from a client on the socket that the embedded application is outputting data to. Basically the web server's purpose is to just let the client retrieve data from the raw tcp socket that the embedded application is writing to. Please advice.
I solved the problem using Websockify, which acts as bridge between a TCP Socket and a browser.
The html client will connect to a websocket, and Websockify will listen on the websocket port and transmit data between the websocket and the tcp socket.
Web browsers have the ability to do HTTP requests (which can be web page requests or Ajax requests for data) and webSocket connections. You will need to pick one of these two mechanisms if you're sticking with stock browser access.
If the lighthttpd web server in the embedded device does not support webSockets, then your choice will like be an Ajax call from the browser to your server. This is basically just an HTTP request that make return something different than a web page (often JSON data) and is designed to fetch data from the server into a web client.
If the lighthttpd web server does support webSockets, then you could use a webSocket connection to fetch the data too. This has an advantage of being a persistent connection and allows for the server to directly send data to the client (without the client even requesting more data) whenever it wants to (more efficient for constant updates).
An Ajax connection is generally not persistent. A client sends an Ajax request, the server returns the answer and the connection is closed. The next request starts a new Ajax request.
Either Ajax requests or webSocket connections should work just fine for your use. All browsers still in use support Ajax. WebSockets are supported in modern browsers (IE10 and higher).
Once you decide upon a client connection strategy, then you'd build your web app on the embedded device that served as the middleman between the browser and the data on the embedded device. It would collect the appropriate data from the embedded device and then be able to send that to browser clients that connected and requested the data.
I'm not sure exactly why you mentioned node.js. In this circumstance, it would be used as the web server and the environment for building your app and the logic that collects the data from your device and feeds it to the requesting web browser, but it sounds like you already have lighthttpd for this purpose. Personally, I recommend node.js if it works in your environment. Combined with socket.io (for webSocket support), it's a very nice way to connect browsers directly to an embedded device. I have an attic fan controller written in node.js and running on a Raspberry Pi. The node.js app monitors temperature probes and controls relays that switch attic fans and node.js also serves as a web server for me to administer and monitor the node.js. All-in-all, it's a pretty slick environment if you already know and like programming in Javascript and there's a rich set of add-in modules to extend its capabilities available through NPM. If, however, your embedded device isn't a common device that there is already support for node.js on or it doesn't already have node.js on it, then you'd be facing a porting tasks to make node.js run on it which might be more work than using some other development environment that already runs on the device like lighthttpd.

Are there any Node WebSocket Server javascript client libraries?

I'm using the node WebSocket Server ( http://static.brandedcode.com/nws-docs/ ) in nodejs for websockets and one java client using this websocket client https://github.com/TooTallNate/Java-WebSocket. I couldn't use socket.io as a server because i couldn't find any java clients for it. Now i've searched for a javascript client for Websocket server, and can't find any. Anyone know of any generic web socket clients in javascript? The socket.io client is not connecting to this server. Thanks for any help.
javascript client for Websocket server
It's called window.WebSocket.
If you want fallbacks and browser support (The thing socket.io is good at!)
Try
web-socket-js
Atmosphere (A completely different framework)
I couldn't use socket.io as a server because i couldn't find any java clients for it
Read the socket.io source, write a Java WebSocket client for it. It should only be a thin abstraction ontop of your Java-Websocket.
Personal advice is to make Java support socket.io. Although I don't see why your doing Java - node.js communication over websockets instead of tcp or udp. The latter two are probably better

Resources