socket.io
socket.io copied to clipboard
Socket.io for golang
Note: for support questions, please use one of these channels: stackoverflow or slack
You want to:
- [ ] report a bug
- [x] request a feature
Current behaviour
https://github.com/googollee/go-socket.io/issues/188
Steps to reproduce (if the current behaviour is a bug)
Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.
Expected behaviour
Setup
- OS:
- browser:
- socket.io version:
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Anyone ?
I also need golang client for socket.io nodejs server. But unfortunately, googollee/go-socket.io is not working for socketio 2.0 and newer.
crickets
Need Golang client too, and also need Rust client. But I can't write code using Rust. I don't know should I use socket.io because I'm not able to develop client for Golang let alone Rust. Someone can give me some advices?
Yeah, I definitely would love to see Socket.IO supporting golang as well. :)
@mofadeyunduo If you're asking whether to use Socket.IO or Websockets alone, I've written an answer on StackOverflow out of my own experience with both of them. https://stackoverflow.com/a/62848079/1712332
If you have the option to make a microservice in NodeJS, or use a Socket.IO library in another language, it's definitely worth it. Websockets are very expensive to make them work on a big complex app and I had a first hand experience on trying to make them work. I mean, if all you want to do is to send back and forth a simple message without having such thing as rooms, or events, then you're good. Use Websockets, it's as simple as doing it on socket.io if not more. But if you want something more complicated, like subscriptions and isolated event rooms, using Websockets means you also have to implement all of those things from scratch and its a pain in the ass.
Need Golang client too
Do need Golang client, too. And I'd like to contribute to it, as soon as I can.
Yeah, I definitely would love to see Socket.IO supporting golang as well. :)
@mofadeyunduo If you're asking whether to use Socket.IO or Websockets alone, I've written an answer on StackOverflow out of my own experience with both of them. https://stackoverflow.com/a/62848079/1712332
If you have the option to make a microservice in NodeJS, or use a Socket.IO library in another language, it's definitely worth it. Websockets are very expensive to make them work on a big complex app and I had a first hand experience on trying to make them work. I mean, if all you want to do is to send back and forth a simple message without having such thing as rooms, or events, then you're good. Use Websockets, it's as simple as doing it on socket.io if not more. But if you want something more complicated, like subscriptions and isolated event rooms, using Websockets means you also have to implement all of those things from scratch and its a pain in the ass.
totally agree with bro
I tried this out and achieved a base result on my local socket.io
server written in golang:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handleConnection(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
fmt.Println("Client connected")
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
fmt.Println(err)
return
}
if err := conn.WriteMessage(messageType, p); err != nil {
fmt.Println(err)
return
}
}
}
func main() {
http.HandleFunc("/ws", handleConnection)
serverAddr := "localhost:8080"
fmt.Printf("WebSocket server is running on ws://%s\n", serverAddr)
err := http.ListenAndServe(serverAddr, nil)
if err != nil {
fmt.Println(err)
}
}
This was my client:
package main
import (
"fmt"
"log"
"net/url"
"github.com/gorilla/websocket"
)
func main() {
// Define the WebSocket server URL
serverURL := "ws://localhost:8080/ws"
// Parse the URL
u, err := url.Parse(serverURL)
if err != nil {
log.Fatal(err)
}
// Establish a WebSocket connection
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
fmt.Println("Connected to WebSocket server")
// Start a goroutine to read messages from the server
go readMessages(conn)
// Send a message to the server
message := "Hello, Server!"
err = conn.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("Error sending message:", err)
return
}
// Keep the program running
select {}
}
func readMessages(conn *websocket.Conn) {
for {
messageType, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Error reading message:", err)
return
}
if messageType == websocket.TextMessage {
handleMessage(msg)
}
}
}
func handleMessage(msg []byte) {
fmt.Println("Received message:", string(msg))
}
Output
- Client side
PS C:\Users\Dell\OneDrive\Documents\GitHub\golang-socketio-client> go run .\go-client.go
Connected to WebSocket server
Received message: Hello, Server!
- Server side
PS C:\Users\Dell\OneDrive\Documents\GitHub\golang-socketio-client> go run .\go-server.go
WebSocket server is running on ws://localhost:8080
Client connected
Of course this is just a basic example. I will need to perhaps create a public repo so that others can contribute to the same / develop it further.
If anyone is interested - my current public github repo