I found that NATS WebSocket connectivity can be a bit tricky to navigate, especially when integrating with game engines or non-Node.js environments. Here’s what I feel you might need to know about the different approaches and probably their trade-offs.
WebSocket Protocol Considerations
When connecting to NATS via WebSocket, remember that raw WebSocket connections require implementing the NATS protocol at the binary level. The WebSocket transport alone isn’t enough – you need to handle the NATS protocol specification for proper message handling.
Available options:
- Native WebSocket + NATS Protocol:
- Requires manual implementation of the NATS protocol
- Best for full control and performance
- Use
nats.ws
library for JavaScript applications - Port 443 for WebSocket connections (vs 4222 for core NATS)
- Socket.IO Bridge Pattern:
- Easier implementation for prototype phase
- Good for environments with existing Socket.IO support
- Works well with game engines like Unreal
- Allows NATS broadcasting through Socket.IO adapter
Important Takeaways
- Don’t try to connect directly via raw WebSocket – implement the full NATS protocol
- Consider Socket.IO adapter pattern for rapid prototyping
- For production JavaScript applications, use the official
nats.ws
library - Native NATS clients (like
nats-c
) operate on port 4222, not WebSocket port
Additional Resources
References
NATS.io. (2024). Official Documentation. Retrieved from https://docs.nats.io
NATS Protocol Specification. (2024). NATS Documentation. Retrieved from https://docs.nats.io/reference/reference-protocols/nats-protocol
NATS WebSocket Client. (2024). NATS.io GitHub Repository. Retrieved from https://github.com/nats-io/nats.ws
Socket.IO Adapter Documentation. (2024). Socket.IO. Retrieved from https://socket.io/docs/v4/adapter/
Games By Sarge. (2024). Unreal-NATS. GitHub Repository. Retrieved from https://github.com/GamesBySarge/Unreal-NATS