Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.io for golang #3242

Open
1 of 2 tasks
rusman-plat-d opened this issue May 8, 2018 · 9 comments
Open
1 of 2 tasks

Socket.io for golang #3242

rusman-plat-d opened this issue May 8, 2018 · 9 comments

Comments

@rusman-plat-d
Copy link

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • report a bug
  • request a feature

Current behaviour

googollee/go-socket.io#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)

@kunaldawn
Copy link

Anyone ?

@ifree92
Copy link

ifree92 commented Jul 14, 2019

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.

@odiferousmint
Copy link

crickets

@mofadeyunduo
Copy link

mofadeyunduo commented Apr 10, 2020

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?

@exapsy
Copy link

exapsy commented Jul 11, 2020

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.

@gzq0616
Copy link

gzq0616 commented Sep 19, 2020

Need Golang client too

@alone-wolf
Copy link

Do need Golang client, too. And I'd like to contribute to it, as soon as I can.

@vinkent420
Copy link

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

@pallasite99
Copy link

pallasite99 commented Sep 10, 2023

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

  1. 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!
  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants