mgold / elm-socketio

[OLD] Socket.io wrapper for Elm.
BSD 3-Clause "New" or "Revised" License
47 stars 8 forks source link

Work with non-socket.io websocket servers #4

Closed hutch closed 8 years ago

hutch commented 8 years ago

I'd like to be able to use this library to talk to pre-existing ws/wss servers that do not use socket.io.

I know you've got socket.io right in the name of the library, but I think you've made statements that this library supports standard websockets. If you meant that the client can use the standard websocket API but the server must still be running socket.io then okay, but I don't quite know why anyone would bother.

Using your Example/websocket server against a non socket.io based websocket server results in a continuous stream of attempts at accessing http://localhost:8001/socket.io/?EIO=3&transport=polling&t=1451837114463-40 which does not exist of course.

Here's a websocket server using https://github.com/websockets/ws, derived from one of their examples, that illustrates the problem:

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ port: 8001 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
    ws.send('echoing: ' + message); // this line is not in the elm-socketio provided server.js file
  });

  ws.send('Hello I am a server using Websockets');
});
mgold commented 8 years ago

I'd like to support standard websocket servers but I'm limited by what socket.io can do. Their docs say that using specific event names will enable compatibility, and this library takes care of that if you pass "" (empty string) as the event name.

That said, the fact that it tries to connect to a /socket.io URL over HTTP is pretty damning for a claim of compatibility.

This library is a pretty thin wrapper, so if you can get a JS client using the socket.io client node module to talk to a ws server, I should be able to make that happen in Elm. But I don't really know how to do the first part. Maybe you can ask the JS project maintainers?

mgold commented 8 years ago

This was discussed further on elm-discuss.

My recommendation is to use ports and the WebSocket API, which boils down to:

var socket = new WebSocket('ws://localhost:8080/');
socket.onopen = function () {
    alert('Connected!');
};
socket.onmessage = function (event) {
    alert('Received data: ' + event.data);
};
socket.onclose = function () {
    alert('Lost connection!');
};
socket.send('hello, world!');