29

Trying to use socket.io-client to connect to a websocket server that is written in Go. I've successfully connected using the node WebSocket library (npm). So the working Websocket code looks like:

  goSocketPort = 6060
  url = "ws://localhost:#{goSocketPort}/streamresults/"
  ws = new WebSocket(url)

  ws.on('open', ->
    log "socket opened"
  )

  ws.on('message', (message) ->
      console.log('received: %s', message)
    #log "Socket message: #{JSON.stringify message}"
  )

Pretty easy and it works -- the socket on the other end sends messages on a set frequency. But I initially tried with socket.io-client (npm) and just couldn't get it to go. It certainly lists websocket as its first-preference transport, but damn if I can get it to connect:

socket = ioClient.connect("#{url}", {port: goSocketPort, transports: ['xhr-polling', 'websocket']}) 

socket.on("connect", (r) ->
  log "connected to #{url}"
)

The connection never happens, so none of the on events are fired and the code exits right away. I've tried: leaving the port off the url and adding it in the options, leaving off the transports option (which means "all" according to the docs) and using an http url. Is socket-io.client not capable of connecting to a "standard" websocket?

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • Have you looked at your network traffic? Socket.io attempts a http handshake before deciding which transport to use; you're probably not responding to the handshake req correctly. – Aaron Dufour Mar 06 '14 at 18:02
  • So you're thinking I'll need to add code to my socket server to get it to work with the socket.io client? – jcollum Mar 06 '14 at 18:16
  • You might be better off not using the socket.io client - what are you using it for? – Aaron Dufour Mar 06 '14 at 18:19
  • Getting messages over a websocket on a periodic (minutes) basis. More than anything I'm curious why this is easy using WebSocket but apparently not easy using Socket-io.client, since I thought SIO was a wrapper for Websocket. – jcollum Mar 06 '14 at 18:23
  • Socket.io is a fairly heavy wrapper. It does transport negotiation, handles a half-dozen or so transports, message types, pings, etc. You're probably going to have to implement large portions of the socket.io server in order to use the client. Why not just use bare WebSockets? – Aaron Dufour Mar 06 '14 at 18:37
  • Like I said, I'm curious why SIO won't work here -- if it's wrapping Websockets, shouldn't it just plug in but give you more functionality on the client side? I can't find any reason that I shouldn't be able to use SIO to talk to a basic Websocket connection. – jcollum Mar 06 '14 at 18:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49171/discussion-between-aaron-dufour-and-jcollum) – Aaron Dufour Mar 06 '14 at 18:43
  • Based on the docs, it cannot use Socket.io client on WebSocket server and vice versa. https://socket.io/docs/ – Exile3daime Jun 26 '19 at 03:38

2 Answers2

47

Based on our chat, it looks like you were misled by this quote:

The socket.io client is basically a simple HTTP Socket interface implementation. It looks similar to WebSocket while providing additional features and leveraging other transports when WebSocket is not supported by the user's browser.

What this means is that it looks similar to WebSocket from the perspective of client/server code that interacts with the Socket.io client/server. However, the network traffic looks very different from a simple WebSocket - there's an initial handshake in addition to a more robust protocol built on top of WebSocket once that's connected. The handshake is described here and the message protocol here (both are links to the Socket.IO protocol spec).

If you're writing a WebSocket server, you're better off just using the bare WebSocket interface rather than the Socket.io client, unless you intend to implement all of the Socket.io protocol.

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
  • 25
    Oh lord thank you. An hour of debugging down the drain before I thought to search for this exact question, I really wish the website a bit more clear that socket.io requires both a client and server side component and cannot interact with a WS only client. It reads the opposite to me - it can communicate with WS or 2-3 other protocols – Hamy Sep 25 '14 at 00:30
  • @Aaron Dufour , i used websocket interface to connect to websocket server . what if i want send data that i receive from the websocket server through my websocket interface to client connected to me through http server , should i use socket.io ? so at the end i will have socket.io attached to to http server and websocket interface to get data and in case of message come will be send to client through socket.io .. PLEASE ADVICE .. – uno-2017 Oct 08 '15 at 22:23
  • @moata_u Please create a new question for that. – Aaron Dufour Oct 09 '15 at 14:54
  • The question created .. http://stackoverflow.com/questions/33052003/send-data-from-websocket-to-socket-io – uno-2017 Oct 10 '15 at 08:38
  • 1
    i'm really glad i looked this up on SO before trying to get it to work. thanks @AaronDufour – RozzA Aug 01 '16 at 03:32
4

Not sure if this was the case at the time, but socket.io's website now states this directly in the docs.

Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

https://socket.io/docs/

J. Schei
  • 327
  • 3
  • 15