- Home /
Multiplayer game using Sockets or Socket.IO? (without UNET or Photon)
Hi,
I'm making a simple multiplayer game with Unity and for the sake of simplicity and maximum control I want to user Sockets.
Game will connect 2 players and get their inputs and apply it on each other's game in real time.
My question is, should I use Socket.IO? Is it better for my use? Because I've Googled around and apparently everyone is using Socket.IO but I've read previously that sockets are just fine.
Also for the server I rather to be able to deploy on Linux machines and don't get tied to Windows.
Any help is appreciated.
Answer by Masoud_A · Oct 23, 2017 at 03:36 PM
Well, it depends!
1- Are you targeting Browsers or Standalone and mobile platforms?
SocketIO is a transport protocol on top of other transport protocols; it uses WebSocket mainly and if websocket isn't supported then uses long polling or other transport options like flash sockets as a fallback.
So if you're not targeting Browsers, you have sockets and you don't need fallback capabilities of Socket.IO.
Also SocketIO has its own communication protocol on top of tcp and websocket, and has some added capabilities like Broadcasting, Reconnecting, message namespacing (like chat rooms) which as you want simplicity and more control, probably you don't need SocketIO for them.
2- Is it a real-time game or turn-based?
If its real-time then milliseconds matter and for sake of performance and speed it’s better to use UDP sockets rather than TCP, and handle message delivery and connection check by yourself. As SocketIO uses TCP for communication maybe it’s not a good choice for high performance real-time games.
If you are interested in establishing UDP communications with C# Sockets in unity, I have developed a Unity package which has necessary classes for establishing UDP connections and some working examples here.
same as 1- For $$anonymous$$obile platform. 2- Real -Time game but delay is not important . I am just trying to learn connections of it.
tcp or udp does not matter for me . thank you anyway. how can we build our socket io ?
Hello, @Masoud_A Thanks for your package. But I have one question.
I am working on a project where I already using photon for networking. Can I use your package for socket io listen? Is it compatible with Photon?
Answer by Valentinon13 · Oct 29, 2016 at 05:05 PM
hi, i made my own server with socket io its running real smooth but i am struggling whit the camera !
but everything works real smooth !
so if its just 2 players its a go i made my server running on a Rasp pi its real nice !
Answer by AIon_unity · Oct 14, 2017 at 04:43 PM
yes sockets are solid, check this one out: http://www.yudiz.com/developers-guide-to-unity3d-mmo-with-node-js-using-socket-io/
Answer by saifuddinazhar · Sep 13, 2018 at 04:18 AM
Yes.. you can use this plugin https://assetstore.unity.com/packages/tools/network/socket-io-for-unity-21721. its free and powerfull, tested on unity editor and android platform. I don't know about other platform. You can run example server included (beep.js.zip) first and then you can run the example scene.
Answer by I5 · Sep 13, 2018 at 04:43 AM
I've found that Tomcat (on the server, using websockets) + HTTP Pro (on the client, deployed in Unity, found in the asset store) provides the best performance, and is the simplest, easiest and quickest to setup (if you're rolling your own server comms). In terms of the "best" performance, I measured milliseconds for packet transmission and message receipt to/from the client and server. Differences between IO an websockets was negligible (a few milliseconds) for plain text messages and HTTP Pro didn't slow things down at all, and it handles TLS and even IO I think. So, I'd go with a tried and well proven solution of websockets (Tomcat server, or Jetty, or a few others) and a 3rd party asset that will connect to your server and not hamstring you into one protocol. HTTP Pro has worked for me (not affiliated with them in any way, it's just saved me a lot of time).