I'm currently developing a chrome extension. I would like to open a tcp socket to a url (for example https://www.google.com), with the goal of establishing a connection to send and receive data. Is there a way I can do this in Javascript? Is it also possible to maintain a connection to a url/server using websockets in Javascript?
Related
I have a server that streams data out a telnet connection that I would like to display on a remote webpage. I can acquire the stream from telnet at a remote site but would like to embed the display on a web page as well.
I realize by implementing via Javascript each client will establish its own telnet connection to display the data.
Is there a way to do this? I tried WebSocket, but it appears it does not support telnet.
I appreciate any guidance.
This question already has answers here:
Sharing websocket across browser tabs?
(9 answers)
Closed 11 months ago.
Some people already have asked this questions in some other places, Im just not sure if it exists here. Anyways, Im using Primus.io with engine.io as it's transformer. Im wondering if it's possible to have shared websocket connection on the browser(client). Like if I connected one client and connect another one on the other tab. Ideally they should get same connection that if I send something through the socket both tabs should be able to get the message.
Other's have mentioned about using the localStorage as a way to share/communicate the same data across different tabs, But I just don't find it neat.
Your help is greatly appreciated.
Best,
Each tab will have a separate connection to the server and has a unique socket id.
If you want to emit a message to every socket for a user id or session id you need to have something to map a user or session to its multiple socket connections.
In Socket.IO, they have a concept of a "room".
On connection you can add the socket to a room. This example uses a passport.js authed username for the grouping.
io.on('connection', function(socket){
socket.join(socket.request.user.username);
});
Then you can send a message to all sockets for that room.
io.to(username).emit('some event'):
Socket.IO cleans up the room on disconnect for you.
https://github.com/cayasso/engine.io-rooms is an implementation of rooms for engine.io that might be useful.
In simple terms
On connection, you want to add the new socket to a list of sockets for a user.
On disconnect, you want to delete the socket from the list of sockets for a user.
On emit, you want to emit a message to all sockets in the list for a user.
If the code in your two tabs cooperate, it is possible to share data from webSocket connection in one tab with the other tab. When a tab opens, it would have to find out if any other tab was already open with an existing webSocket connection and it would have to then ask that tab to share its data with the new tab. This is not a trivial operation as browser windows don't have an easy way to find out about other browser windows unless the new window was opened by the prior window's script, not opened by the user.
But, if each of your windows just attempts to make a webSocket connection to the server, they will each get their own webSocket connection. There is no automatic sharing of webSocket connections between tabs.
FYI, the usual behavior here is that each window just gets its own webSocket connection to the server and then the server just separately communicates with each browser window. If some information from one window needs to be kept in sync with the other window, then you can code your server to keep both up-to-date in that way.
I'm playing around with an open source project that uses Socket.IO to connect to the website chat.meatspac.es which is hosted on a different server. It connects like this
var socket = socketClient.connect('https://chat.meatspac.es');
And then it listens for the data that is also published on chat.meatspac.es. Does this only work because the chat.meatspac.es is also a Socket.IO application and it's emitting signals that can be picked up by this other open sourced project?
For example, I naively tried to connect to my Twitter stream
var socket = socketClient.connect('https://twitter.com/username');
but it wouldn't listen for messages published in my Twitter feed.
So, my question is, can Socket.IO only connect to servers that are emitting Socket.IO messages?
socket.io allows simulation of socket API (used in traditional desktop apps) for access with javascript (web).
But this is will not behave exactly as traditional sockets behave. It uses HTML5 websockets for sockets simulation
Sockets.io needs to have a compatible server app, that is shown here by deploying a server app using nodejs
Take a look at server side implementations of websockets today, socket.io with nodejs being one of them.
http://www.html5rocks.com/en/tutorials/websockets/basics/
So for your question using socket.io to create a socket connection to twitter:
var socket = socketClient.connect('https://twitter.com/username');
This is not possible because socket.io is not a real socket API, its just a simulation of sockets api using HTML5 websocket api and maybe other fallbacks (long polling, comet, etc). So the server should be compatible to handle the ws(websocket protocol) request and in case of twitter here to get feed stream, this is not the case so this can not be done like that.
See the twitter api to programmatically get access to twitter resources
https://dev.twitter.com/docs/api/1.1
What I'm trying to do is create and communicate to an embedded wifi device (much like an arduino board) that doesn't have any frills at all. It basically accepts a socket and listens for information and responds.
The complete extent is that I'm using PhoneGap in order to make the application cross device compatible.
I had planned to try and make TCP sockets using a javascript plug in of some kind, however all of the plugins i've come across require server side scripts (not possible in this case). I can always put a gateway computer between the android and iOS devices, but that seems inefficient if I can just create a browser socket.
So my question is does anyone know of a javascript plugin that can create TCP sockets without server side scripting?
If not, do you think it would be possible for me to create a plug-in for PhoneGap that makes calls to the native SocketHandler Code?
Actually, do they already have a plug-in that would allow you to use the Native TCP Sockets via javascript?
I hope my questions makes sense. Basically looking for a way to send small serial communications via TCP/IP over a web browser on a mobile device.
Anybody know abou this?
There are a few plugins available for PhoneGap networking. Three of them are:
iOS, Android, WP8: https://github.com/blocshop/sockets-for-cordova
Android: https://github.com/Habel/TCPSockets
iOS: https://github.com/purplecabbage/phonegap-plugins/tree/master/iPhone/GapSocket
These should give you network-layer sockets that can send TCP requests.
As for WebSockets: WebSockets (hereafter "WS") are an application-layer protocol, like HTTP or XMPP. You can implement WS using TCP yourself, since WS sits on top of TCP.
If Android's WebView can send WS requests natively, you should use that instead. WS is nothing more than a protocol that sits on TCP and looks kind of like HTTP. Just use a WebSocket server library like ws2py for Python or ws for Node.js. Only use TCP if you need to connect to an existing service that doesn't understand or accept WebSockets.
Is it possible to create a connection to a redis server from the browser? You can spin up a TCP connection in node using var client = net.connect(port, host);. I'm wondering if there is anyway to do this in the browser using Browserify or a native way to connect to the server?
Thanks!
There's no way to do this directly from the browser, but you can make TCP connections with a browser plugin.
Check this url for more info on this: http://news.ycombinator.com/item?id=3972368