LongHike
LongHike

Reputation: 4460

How to use socket.io browserify?

How do I use socket.io with browserify?

When socket.io is installed in a node app, it creates this url path for ths socket.io script:

/socket.io/socket.io.js

But what would be the real path of that file (relative to the node_modules dir) which needs to be passed to browserify?

Is it e.g.

socket.io-client/socket.io.js

or maybe

socket.io/lib/client.js

In the documentation of socket.io-client it says "Socket.IO is compatible with browserify." But they don't say how.

Upvotes: 3

Views: 2322

Answers (4)

Martin Todorov
Martin Todorov

Reputation: 150

If you struggled to get it working with browserify as a window global use this code for the integration:

var io = require('socket.io-client');
window.io = io;

Upvotes: 3

SteamDev
SteamDev

Reputation: 4404

Import client and then assign it to variable with:

var client = require('socket.io/lib/client');

You can then call client.

Upvotes: 0

robertklep
robertklep

Reputation: 203359

Here's a minimal client:

// client.js
var socket = require('socket.io-client')();
socket.on('message', ...);

Which you can Browserify:

browserify client.js > bundle.js

Upvotes: 2

Loic Coenen
Loic Coenen

Reputation: 853

The path will be exactly the same since it's the server who serve the socket.io client library (and I guess you're not browserifying the server, are you?).

But I use a more convenient solution: check this out.

io  =   require 'socket.io-client'

class Network

    constructor: (game, refresh_infos) ->

        @sock = io()
        #...

    pending: (name, cb) ->

        @name = name
        @sock.emit 'pending', name: name
        @sock.on 'new_game', (data) => cb data

Abrakadabra!

Upvotes: 0

Related Questions