import WebSocket from 'ws' in browser - javascript

I am a Korean developer.
I'm making a game and a game bot now.
Games run on WebGL and gamebots run on node JS.
There is a game git in the gamebot git. Because the game bot git has only scenario and is basically the same as normal game
The problem is, I'm using WebSocket in the code in this game git, but I imported the ws library because Node Js does not support WebSocket.
But,
import WebSocket from 'ws';
This code causes an error in the browser.
What should I do to use the websocket in both Nodejs and Browser with the same code?

You can't use a websocket on the back and the frond end with the same code if only because they do different things. In the browser you don't need to import anything as websockets are supported natively. You can read on it in MDN: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

Related

Cannot connect to mosquitto broker from local web client on windows

I am trying to connect to mosquitto windows broker service on my machine through javascript. I understand that Paho javascript client connects only through websockets and mosquitto for windows doesn't support websockets unless you build it yourself. My question is that since I am trying to connect to localhost should it matter to have a websockets enabled mqtt broker? Is there any way to connect to a local mqtt server without using websockets through javascript? And is it unsafe to do so or is it fine since I am connecting to a local server itself?
Thanks in advance for the answer and sorry if it sounds lame to you. I am extremely new to this space.
Even when connecting to localhost you still need websockets support to use the paho JavaScript support.
There are some small apps that will set up a separate websocket listener and bridge that to pure native MQTT e.g.
http://hai-ng.blogspot.co.uk/2014/08/setting-up-mqtt-websocket-gateway-with.html

Connect to OpenVPN server through Node.js

I’m trying to create a GUI client for connecting to OpenVPN servers using electron and node but I’m struggling to figure out how to actually connect to the servers using the .ovpn files.
My question is what is the best way to connect to an OpenVPN server using node? Would it be best to Tun terminal commands like
“openvpn—config path to config”
Or is there another way applications like tunnelblick do it that might be easier or more efficient?
Hello I have been working with electron and ovpn on my last project, so here are a few tips.
VPNs require admin/root privilege in order to get setup, so running child_process.spawn on openvpn --config <path> will fail unless your electron app is being ran via sudo/admin privilege.
You can also use electron-sudo package, link here. This is basically a child process spawn with sudo/admin. Aka, app runs normally, but vpn command runs with sudo.
However, if your client is sketchy about giving you sudo/admin, the VPN must be ran separately prior to launching your app.
All in all its a admin/sudo thing.
Hope this helps.

Having problems running WebSocket for mqtt

I'm trying to replicate this demo
using my own mosquitto instance to control the meter. When I try to connect to "test.mosquitto.org", 8080 everything works great, but when I plug my own mqtt server's IP I receive: Connection failed: AMQJ0007E Socket error:undefined.
I believe this is due to the requirement of a websocket service to run on the same machine as the mqtt. The original author uses WSS, and I can't figure out how to install this service (complete noob). I Even tried to follow these directions with no luck Could anyone give some directions?
You can try Installing him HiveMQ while you wait for mosquito 1.4. That is a broker with websockets built in.
The easiest answer to this is probably to wait for Mosquitto v1.4 to ship which has baked in Websocket support.
If you can't wait for it to ship you can build from the development head stream which will get you what your looking for. You've not said what platform you are running the broker on, but these instructions should help on Linux: http://goochgooch.wordpress.com/2014/08/01/building-mosquitto-1-4/
Mosquitto v1.4 has been released on February 18th, 2015. It has inbuilt support for websockets.

Installing NodeJS and SocketIO in a Remote Server

My project is a Real Time Two-Player Facebook Game, and what I need is a tool that will help me build the game with quick responses to enable the "Real Time" function of the game. I have just found out about the Node JS and Socket IO. I have some knowledge in JavaScript so I stepped up and watched a few tutorials that discuss the functions of Node JS and Socket IO.
Here's the link to the videos that I have watched:
http://www.youtube.com/watch?v=mSE6xHkcX0w
I understand the basic of the Node JS and Socket IO and successfully installed it in my localhost. The problem is when I uploaded the files from my localhost to my remote server, some functions of the program are not working well. I don't know how to node my JavaScript file when it is on the server, because if it's in my localhost, I am using command prompt to run it.
node app.js
Node is not a web framework.
Chances are, you're using a web host that's generalized for web frameworks like PHP and Ruby on Rails. You're going to need virtual private server hosting, or Node-specific hosting, because Node requires a virtual machine to run. You otherwise won't be able to run Node Package Manager or Node itself.
Joyent has provided a list of hosts here.
If you chose to use a VPS or dedicated machine, an installation guide would be found here. This is how you would install Node on CentOS.
wget http://nodejs.org/dist/v<version>/node-v<version>.tar.gz
tar -zxf node-v<version>.tar.gz
cd node-v<version>
./configure
make -j <number of cores>
make install

Installing/setting up Socket.IO on my server

Ok so I have read through the Socket.IO docs and I am still a little unsure of a couple of points:
The documentation says...
To run the demo, execute the following:
git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io
cd socket.io/example/
sudo node server.js
Now I don't know what this means at all! I think it may be command line interface. I of course have access to this on my localhost, but my online hosting package is a shared LAMP setup. Meaning I don't have access to the root command line (i think).
How do I actually setup socket.IO, is it impossible on my shared server package?
Appreciate any help...
W.
If you aren't familiar with node.js or with basic command line usage then I would suggest that you use a hosted WebSockets solution like pusherapp. Trying to learn WebSockets, and Node.js, and the Linux command line all at once is going to lead to a lot of frustration. Take a look a pusherapp's quick start guide, it's very easy to get started. You can have 5 simultaneous connections with a single application for free (I'm not affiliated with pusherapp).
Updated (with inline answers to questions):
If you are going to go the direction of running a Socket.IO application:
You don't technically need git since you can download node.js and Socket.IO from their respective download links on github.
You don't actually need a LAMP server to use Socket.IO. By default Socket.IO functions as a simple webserver in addition to a WebSockets server. If you want server side scripting then you might want Apache with mod_php, mod_python, etc.
You don't technically need a dedicated server or even root access. You do need a system where you can have long running process. And if you want the service to start automatically when the system is rebooted, you probably want to add a startup file to /etc/init.d, /etc/rc.d which will require root access. Both node.js and Socket.IO can be installed and run from a normal home directory. If you want to run Socket.IO on a standard port like 80 or 443 then you will need to run it with root privilege.
Node.JS scales quite well so Socket.IO will probably scale pretty well too.
It's not a simple matter to get everything setup and working, but if your goal is a free solution for web serving+WebSockets then Socket.IO is probably is good route to at least explore if you are brave.
First you'll have to determine if your host supports SSH. Sometimes they don't by default on shared hosting, but if you ask they can turn it on. If it does you'll use some sort of SSH client to connect to it. Putty for windows is the most common. Then you'll use git, which is a source control program. Which you'll probably have to install on your host, which may or may not be allowed. If you can, this can be accomplished a number of ways, you'll want to read the git documentation, it will depend largely on what linux distribution you're running. CD is change directory, basic command line stuff. sudo on the last line is telling the system to run the command as root, which it will ask you the password for, which you may not have access to on your host. Sounds like you're gonna have an uphill battle on shared hosting. You may want to opt for a VPS instead.
If your shared host is a LAMP system with no command line access you're not going to get very far with Socket.IO. The instructions you posted assume you have command line access and that you've installed the node.js runtime on your system.
If you really want to try this I recommend you get a VPS of your own (I use prgmr.com) to test it out. For what it's worth I found the Socket.IO platform pretty nice to use once I got it up and running.

Categories