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

Primus.library() + SockJS client does not work in electron. #590

Open
MrBlenny opened this issue May 19, 2017 · 6 comments
Open

Primus.library() + SockJS client does not work in electron. #590

MrBlenny opened this issue May 19, 2017 · 6 comments

Comments

@MrBlenny
Copy link

MrBlenny commented May 19, 2017

Version: Latest

Environment:

  • Operating system: Windows 10
  • Node.js: Electron (Node thread) with Webpack

I'm building the client library using Primus.library() with sockJs as the transformer.
The output does not work in electron. You get the following error:

TypeError: Cannot set property 'SockJS' of undefined
    at a (\primusSockjs.js:3312:103)

This corresponds to the following line:

3312: (function(f){var g;if(typeof window!=='undefined'){g=window}else if(typeof self!=='undefined'){g=self}g.SockJS=f()})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){

The issue is that both window and self are undefined. There is no else case.

I've put in else{g=this} This seems to fix the issue for now.

3312: (function(f){var g;if(typeof window!=='undefined'){g=window}else if(typeof self!=='undefined'){g=self}else{g=this}g.SockJS=f()})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
```
@lpinca
Copy link
Member

lpinca commented May 19, 2017

This is because deUMDify removes this branch https://github.com/ForbesLindesay/umd/blob/527ff2920db6a1330623157faced5eed45b765b6/template.js#L19-L24.

Unfortunately I don't remember why it was needed :/

Can't you use the Node.js client?

const Primus = require('primus');

const Socket = Primus.createSocket({ transformer: 'sockjs' });

const primus = new Socket(url);

@3rd-Eden
Copy link
Member

Electron really has 3 JavaScript environments:

  1. The nodejs side.
  2. The electron/chromium side
  3. The electron/chromium with webPreferences.nodeIntegration set to false.

So which environment are we talking about here? In your original issue you described it as "node" but there's different flavors of "node" here.

@MrBlenny
Copy link
Author

This was in the nodejs side of electron - i.e. the main thread (not a renderer/browser).

I'm not using the NodeJs client because the dynamic requires do not work with webpack. In most electron projects the main electron thread will not be built with webpack, in those cases the node version would work fine. Unfortunately I have to use webpack to package the main-thread also.

@lpinca
Copy link
Member

lpinca commented May 20, 2017

@MrBlenny did you try excluding the SockJS client from the bundle?

const lib = primus.library(true); // => build for Node.js
fs.writeFileSync(path, lib, 'utf-8');

and use the result with webpack. I didn't try it but theoretically it should work.

@madshargreave
Copy link

Did you figure this one out? @MrBlenny

@MrBlenny
Copy link
Author

I couldn't get SockJS to work in both environments. I made a few changes to the websockets lib (I forget what) - it works in both environments but of course doesn't have the nice browser fallbacks.

https://github.com/stemn/stemn-frontend-shared/blob/master/src/misc/Websocket/primus-websockets2.js

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

4 participants