I have been thinking of making a real-time game with WebSockets for the web. I know how to use Node.js, and it is tempting to make it on there. But everywhere I look, C++ seems to be the popular server language because of its speed.
Should I give making it in Node.js a go, and worry about C++ later, or should I learn C++ now and make it in there from scratch?
I have been thinking of making a real-time game with WebSockets for the web. I know how to use Node.js, and it is tempting to make it on there. But everywhere I look, C++ seems to be the popular server language because of its speed.
Should I give making it in Node.js a go, and worry about C++ later, or should I learn C++ now and make it in there from scratch?
Share Improve this question edited May 12, 2016 at 21:54 Luke asked Apr 25, 2016 at 2:35 LukeLuke 2,1685 gold badges25 silver badges49 bronze badges 2- Unless you're doing CPU-bound work, Node.js is probably fine. – SLaks Commented Apr 25, 2016 at 2:39
- @SLaks, say I was making Agar.io, which would be better? – Luke Commented Apr 25, 2016 at 2:40
2 Answers
Reset to default 11If you do decide to go the C++ route (and that does offer the best performance of any language), there's this great open source Websocket library that does all the heavy lifting for you. Its header-only and uses just boost. It es with example code and documentation: http://vinniefalco.github.io/
Here's a plete program that sends a message to the echo server:
#include <beast/websocket.hpp>
#include <beast/buffers_debug.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main()
{
// Normal boost::asio setup
std::string const host = "echo.websocket";
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r(ios);
boost::asio::ip::tcp::socket sock(ios);
boost::asio::connect(sock,
r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));
using namespace beast::websocket;
// WebSocket connect and send message using beast
stream<boost::asio::ip::tcp::socket&> ws(sock);
ws.handshake(host, "/");
ws.write(boost::asio::buffer("Hello, world!"));
// Receive WebSocket message, print and close using beast
beast::streambuf sb;
opcode op;
ws.read(op, sb);
ws.close(close_code::normal);
std::cout <<
beast::debug::buffers_to_string(sb.data()) << "\n";
}
Google's V8 engine used for Node.js does a great job piling effective machine code. Javascript gets good enough performance to be used in games, aside from special attention required on memory/garbage collection., and this lead to a lot of native C++ PC games being converted into browser javascript games. (notably, humble bundle ran a "Mozilla Bundle" which had a lot of these converted JS games, including "AaaaaAAaaaAAAaaAAAAaAAAAA!!! ", "FTL", etc.)
A lot of real-time games servers are created with Node.js along with socket.io. It is easy to get a basic websocket server running with node and socket.io, so you should be able to quickly build a prototype server, and do some performance testing to see if it will be sufficient for your application.