Cannot connect to API: Should I use websocket or something else? - javascript

I have this API: http://developers.xstore.pro/documentation/2.2 which says:
Communication with the xStation API
There are two IPs, that can be used interchangeably:
xapia.x-station.eu
xapib.x-station.eu
Here are the addresses of DEMO and REAL servers:
DEMO: main port: 5124, streaming port: 5125
REAL: main port: 5112, streaming port: 5113.
Both servers use SSL connection.
I'd like to login to my account but I just don't know how to connect to this API. I figured, the right way might be to use websocket which I've never used before...
websocket = new WebSocket("ws://xapia.x-station.eu:5124");
...but didn't even connect to server.
Could anyone provide me with some simple example or at least point me to right direction? This is completely new to me and don't know where to start.

I'll make this into an answer since it looks like it answers your question.
The API you refer to looks like it uses a plain SSL TCP connection (not a webSocket) and you must send requests as properly formatted JSON. If you were connecting to this API, you would use a plain socket as described in the nodejs Net module.
Cannot connect to API: Should I use websocket or something else?
You should be using a plain TCP socket, not a webSocket. A webSocket is a higher level protocol that runs on top of a TCP socket. It can only connect to a webSocket server.

Related

Do any public XMPP servers use websockets?

I would like to use some simple Javascript code I have written, with any public XMPP server using WebSockets. I have seen multiple examples of this and wrote my code accordingly. I am not using any javascript or XMPP libraries and would prefer to keep it that way.
I am opening my websocket with socket = new WebSocket("ws://xmpp.xyz"); and have tried over a dozen servers. My application does not need encryption and therefore doesn't use wss://.
The error returned in chrome's console is "WebSocket connection to 'ws://xmpp.xyz/' failed:". I have been able to successfully open a websocket to the external echo server echo.websocket.events/.ws. Using chrome's network inspect, I see the socket open successfully and my stanza's being sent and received correctly with callback functions.
I now wonder if all the examples I had read, were for servers being hosted in a company network, and none of the public XMPP servers will accept my websocket connection request. If they can accept my request is there something else I need in my url? Based on some of the examples, I have tried a few ports like 80, 5280 and some additions to my url (such as "ws://jabber.org:80/ws/").
Yes, at least one public XMPP server, Tigase.im, does support websockets.
After some light digging though the strophe.js code, I found a second parameter that specified the service as a string. Opening the websocket in pure javascript with the port provided by Wojtek and the protocol string set to xmpp worked.
socket = new WebSocket("wss://tigase.im:5291/","xmpp");

Where does all data send with nodejs method socket.write?

Good day! Where does all data send when nodejs method socket.write is called? I understand that socket runs of the server side for each client. But where exactly does data go? to client? On official nodejs documentanion there is no info about destination. Thank you for response.
You cannot successfully write to a socket unless you (or some part of your nodejs software) first connects it to some other socket somewhere.
A socket server listens for connection requests, and then accepts them as they arrive. (When you use node express to make a web server, express handles this for you.) A client connects to a socket server. Once the pair of sockets are connected, data you write into one of the sockets causes a data event on the other one.
The two sockets may be on different machines in different locations. That's the miracle of global networking.
So where does data you write go? To the other socket in the pair.
If you are using datagrams (not connections) it's slightly different. The data you write contains the destination address. But you probably are not using databgrams. If you are, you are probably using a protocol stack like RTSP or UDP instead of TCP.

Javascript Vert.x EventBus Client SockJS get host and port of connection

I am using Vert.x Core 3.3.3 and Web 3.3.3 in Java as an EventBus server and Vert.x 2.0.0 and SockJS 1.1.1 on a web client in Javascript to connect to the EventBus. I am connected, able to send and receive messages, and all inbound and outbound traffic is open. Everything is functioning properly.
My server is able to get the host and port for each EventBus connection when the client connects. This comes from the SockJSHandler's bridge event in Java, via socket().remoteAddress() in the format "host:port".
On the web client side, is there functionality to get the remote address (host and port of the server) of the EventBus SockJS connection to my Vert.x Server's EventBus? I have looked in the documentation for Vert.x Web Client (http://vertx.io/docs/jsdoc/ & https://github.com/vert-x3/vertx-web) but have not found anything useful.
If you need more info please let me know. Thanks in advance.
EDIT 1:
I misunderstood what I was actually looking for in my original question and phrased it poorly. I know what the host and port of the EventBus are when I make the connection (i.e. var eb = new EventBus('localhost:8080/eventbus'); as #Paulo said).
What I am actually looking for is the host and port for the Handler that my client registers on that EventBus. I can see this on the server side as mentioned above. The host comes back as an ip address, not what I passed in to create the EventBus connection on the client-side. And the port is different, because a new SockJS connection is made for that handler (?). Is it possible to get the host and port, especially port, of the SockJS connection for my client's Handler.
The information you want to have is something you already know. When you create a eventbus connection from your client web application you do:
var eb = new EventBus('http://localhost:8080/eventbus');
As you can see the parameter you must pass to the constructor contains what you want to know:
localhost:8080
Most of the times you do not want to use an hardcoded hostname + port but the location where your html has been served so in JavaScript you can just do:
window.location.host
And that will give you the location where your server is running.

how to not expose server info when loading and connecting to socket

I have drupal site that communicates with Node.js server via sockets, so i have the following to load and send connection request in the header:
<script src="http://xx.xx.xx.xx:3000/socket.io/socket.io.js"></script>
...
socket = io.connect('http://xx.xx.xx.xx:3000');
This works well, however. I prefer my server ip and the port number is not exposed to the rest of the world. What is the approach to do so? Thank You
The short answer is :
You can not do this.
The longer explaination :
Your socket.io connection is like any other connection made to your server(s). It will be always visible in the developer tools of any other browser. You might want to use a proxy service for that, but in the end your socket.io server will always be exposed.
Nobody will ever read your JS code if he wants to get your socket.io IP-address, there are many other easier ways to do that.
Although you can always use the autoconnect feature of socket.io and just put
socket = io.connect();

Javascript port access

Can javascript access the system ports ?
If yes, is it possible to write a server (TCP/UDP in C) to which data can be send from javascript ?
Let's assume that you're not asking for Node.js.
You can write WebSocket server in C and connect your JavaScript to it. There are already plenty of implementations so you don't have to write it yourself.
When you start your server and bind it to specific host name and port you can connect from your JavaScript by:
var ws = new WebSocket('ws://hostname:port');
ws.onopen = function () {
console.log('Connected');
};
ws.onmessage = function () {
console.log('New message...');
};
ws.send('Some message...');
For example, here is one http://code.google.com/p/cwebsocket/.
As far as I know WebSocket is over TCP.
One more option is to create HTTP server use AJAX with or without long polling.
What do you mean by "system ports"? Serial ports? AGP ports? Rewrites of an application in a different programming language? There is no such term as "system ports" in general usage. You seem to be referring to sockets.
In which case....
Sort of. You can get a connection to any TCP port using javascript - but you will only be able to communicate over that port using HTTP. It is possible to tunnel communications via websockets - however this requires a proxy at the server end to connect to a conventional server. TCP is a stream protocol but websockets are message oriented.
If you're implementing your own server from scratch, then it's a lot easier to build on top of something which already understands about websockets (such as node.js).
You can't use UDP (other than DNS lookups as a by-product of establishing TCP connections).

Categories