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

Add new server #443

Merged
merged 32 commits into from
Mar 11, 2021
Merged

Add new server #443

merged 32 commits into from
Mar 11, 2021

Conversation

timmydoza
Copy link
Contributor

@timmydoza timmydoza commented Mar 2, 2021

Contributing to Twilio

All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.

  • I acknowledge that all my contributions will be made under the project's license.

Pull Request Details

JIRA link(s):

Description

This PR adds a new token server to the React app, which can be used for local development and to drive the Cypress E2E tests. This server imports the token function from the RTC Twilio CLI Plugin. This means that this server has the same API and functionality as the Serverless Functions that are deployed by the CLI plugin.

With this change, this server now has support for Twilio Conversations, which means that it will now be possible to build the Conversations UI and test it in Cypress.

Burndown

Before review

  • Updated CHANGELOG.md if necessary
  • Added unit tests if necessary
  • Updated affected documentation
  • Verified locally with npm test
  • Manually sanity tested running locally
  • Included screenshot as PR comment (if needed)
  • Ready for review

Before merge

  • Got one or more +1s
  • Re-tested if necessary

Copy link
Collaborator

@charliesantos charliesantos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of comments. Also, I'm on the fence on this approach where we are mocking some globals so that the serverless function works in an express environment. If we continue to follow this, it means future development mindset would be:

  • Write serverless function and make sure it works in the serverless environment.
  • Update express app and mock any globals so that the new serverless function works ok.
  • Any new updates on the function that might break the express app (e.g. adding new globals) requires updating the express app.

I think the right approach would be to write functions/endpoints that works on both platforms (serverless and express) that way we don't have to mock globals. If you look at other cross platform code, this is usually how they handle this. Thoughts?

package.json Outdated
"server": "node server.js",
"lint": "eslint src server",
"server": "ts-node -T -P server/tsconfig.json server/index.ts",
"build:server": "tsc -p server/",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to compile the ts files if we are already using ts-node?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this script so that we could check for any errors during our Circle CI builds. I suppose we only need to perform type checking, not compilation. I'll change the script name to better describe this and also add --noEmit.

process.env.TWILIO_ACCOUNT_SID = 'mockAccountSid';
process.env.TWILIO_API_KEY_SID = 'mockApiKeySid';
process.env.TWILIO_API_KEY_SECRET = 'mockApiKeySecret';
process.env.TWILIO_CONVERSATIONS_SERVICE_SID = 'mockConversationsServiceSid';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update .env.example


const app = express();
app.use(express.json());

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add CORS headers?

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', '*');
  next();
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - just curious though, why would we want to add them? I'm planning on hosting the front-end assets from the same origin as this server, so I don't think we would need them.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking for dev purposes only. I usually have different frontend accessing different servers locally. This is one of them :)
But it's ok not to add them if we're going to deploy this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. When the app is run in dev mode with npm start, the api requests made to /token will be proxied to localhost:8081/token. So from that app's point of view, everything is on the same origin in dev mode. So I don't think we'll need them for that. But if we ever need them in the future I'm happy to add them in.

@timmydoza
Copy link
Contributor Author

Left a couple of comments. Also, I'm on the fence on this approach where we are mocking some globals so that the serverless function works in an express environment. If we continue to follow this, it means future development mindset would be:

  • Write serverless function and make sure it works in the serverless environment.
  • Update express app and mock any globals so that the new serverless function works ok.
  • Any new updates on the function that might break the express app (e.g. adding new globals) requires updating the express app.

I think the right approach would be to write functions/endpoints that works on both platforms (serverless and express) that way we don't have to mock globals. If you look at other cross platform code, this is usually how they handle this. Thoughts?

I definitely understand the concern here, but I think that it will be somewhat rare that we will run into steps 2 and 3 from the list above. When we update our Serverless functions in the future (or add new ones), I think it is unlikely that we will need to update the global variables. We might need to add new assets here, but other than that I can't think of any globals that we would need to add or change in the future. It's possible that I missed one or two, but I don't think that adding them in later would be a high maintenance cost.

I think the right approach would be to write functions/endpoints that works on both platforms

I agree that this would be ideal, but unfortunately, I don't see how it's possible. The only way to change the status code and headers of a response from a Twilio Serverless function is to use the global Twilio.Response. Because Twilio Functions have a hard dependency on the Twilio global, I don't see how we can get around mocking it if we want these functions to run in Express too.

In my mind, the main thing that we want to avoid in building a new server is duplicating business logic (and tests for the same) across multiple repos, which is what we are currently have with video-app-service and plugin-rtc. With the approach in this PR, all of the business logic (and tests) live in plugin-rtc, and this server has code to run them as part of an Express server. So even though we may need to update these globals in the future, I think that we are still saving quite a lot in terms of long-term maintenance costs.

@charliesantos
Copy link
Collaborator

Thanks for addressing my concerns! let me know once this is ready for another look.

@timmydoza timmydoza merged commit 89d98fd into feature/conversations Mar 11, 2021
@timmydoza timmydoza deleted the server-prototype branch March 11, 2021 18:13
timmydoza pushed a commit that referenced this pull request Mar 31, 2021
* VIDEO 3731 Conversations UI - create chat window (#421)

* Video 4078 add flip camera button to mobile menu (#433)

VIDEO 4078 Conversations - Add flip camera button to 'more' mobile menu

* Video 3730 add conversation hooks (#438)

* Add initial hooks for chat

* Update hooks in chat provider

* Add tests for conversationProvider component

* Fix DeviceSelectionScreen tests

* Update variable names

* Revert proxy url in package.json

* Fix linting issues

* Make error handling more user friendly

* Fix a test in src/state

* Temporarily disable cypress tests

* Undo prettier changes to config.yml

* Update chatProvider test

* Cleanup ChatProvider

* Video 3732 Conversations UI - create message components (#441)

* Add links to TextMessage component (#446)

* Add links to TextMessage component

* Update CSS in TextMessage

* Update snapshot test

* Fix issue in addLinks function

* Use forEach loop in addLinks function

* Add new server (#443)

* Use new server with reused code

* Add firebase auth to server

* Convert server to typescript

* Add tests for firebaseAuthMiddleware

* Refactor server

* Add more server tests

* Add tests for CreateExpressHandler

* Fix linter issue

* Delete old server

* Fix linter issues

* Fix some server issues

* Set up server build steps

* Change request to match new server api

* Fix token fetch test

* Add server build step to circleci config

* Enable cyress tests in CI

* Undo prettier changes in config.yml

* install plugin-rtc from npm

* Fix firebaseAuth test

* Add create_conversation:true to token requests

* Upgrade plugin-rtc to 0.8.0

* Temorarily remove firebaseAuthMiddleware and recording rules route

* Add ChatProvider back in

* Update .env.example

* Change build:server script

* Add firebaseAuthMiddleware

* Add comments to firebaseAuthMiddleware

* Update useFirebaseAuth tests

* Video 3728 Conversations UI - add input field and send button (#451)

* Chat notification (#454)

* Add notification to chat icon

* Add tests for ToggleChatButton

* Some cleanup

* Fix linting issue

* Video-4525 loading screen (#461)

* Video 4454 Conversations UI -  message list scrolling (#467)

* Update MessageList component to change how messages are grouped (#469)

* Update MessageList component to change how messages are grouped

* Simplify MessageList code

* Video 3888 file input for conversations (#458)

* Add FileAttachmentIcon

* Add File Input to ChatInput component

* Add change handler to file input

* Create MediaMessage component and add it to MessageList

* Toggle ChatWindow visibility with CSS

* Update messageList tests

* Add function to format the file size

* Update error handling in ChatInput component

* Add tests for MediaMessage component

* Update snapshot test

* Add tests for ChatInput

* Fix linting issue

* Update filesize formatting

* Update error language and tests

* Reset file input value after file sent

* Center icon in MediaMessage

* Update Icon size

* Fix breaking ChatInput test

* Update snapshot

* Use accept attribute on file input element

* Update readme to add information about the chat feature and conversations api

* Cross browser fixes (#472)

* Update app.yaml for the new server

* Add cache controls for static assets

* Only hide Screenshare button on mobile devices

* Auto focus on chat input when user opens chat window

* Fix iOS bug with media messages

* Fix issues in MessageListScrollContainer

* Fix app.yaml

* Fix infinite loop when there is a conversations error

* Update tests for ChatInput

* Update mediamessage test

* Update menubar tests

* Update messageList snapshot

* Fix linting issue

* Add comments to the server

* Video 3727 - cypress testing (#473)

* Video 4597 feature flag (#475)

* Update plugin-rtc version

* Add feature flag for Twilio Conversations

* Add tests

* Add info to .env.example

* Update readme with info about twilio conversations (#476)

* Update readme with info about twilio conversations

* Update readme to address feedback

* Release 0.3.0 (#479)

* Bump version and update changelog

* Add plugin-rtc upgrade instructions to the readme

Co-authored-by: olipyskoty <77076398+olipyskoty@users.noreply.github.com>
Co-authored-by: Sean Coleman <smcoleman27@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants